
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>The Django admin site &#8212; Django 1.11.22.dev20190603194737 documentation</title>
    <link rel="stylesheet" href="../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" />
    <link rel="next" title="Admin actions" href="actions.html" />
    <link rel="prev" title="contrib packages" href="../index.html" />



 
<script type="text/javascript" src="../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../index.html">Django 1.11.22.dev20190603194737 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="../index.html" title="&lt;code class=&#34;docutils literal notranslate&#34;&gt;&lt;span class=&#34;pre&#34;&gt;contrib&lt;/span&gt;&lt;/code&gt; packages">previous</a>
     |
    <a href="../../index.html" title="API Reference" accesskey="U">up</a>
   |
    <a href="actions.html" title="Admin actions">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="ref-contrib-admin-index">
            
  <div class="section" id="s-module-django.contrib.admin">
<span id="s-the-django-admin-site"></span><span id="module-django.contrib.admin"></span><span id="the-django-admin-site"></span><h1>The Django admin site<a class="headerlink" href="#module-django.contrib.admin" title="Permalink to this headline">¶</a></h1>
<p>One of the most powerful parts of Django is the automatic admin interface. It
reads metadata from your models to provide a quick, model-centric interface
where trusted users can manage content on your site. The admin’s recommended
use is limited to an organization’s internal management tool. It’s not intended
for building your entire front end around.</p>
<p>The admin has many hooks for customization, but beware of trying to use those
hooks exclusively. If you need to provide a more process-centric interface
that abstracts away the implementation details of database tables and fields,
then it’s probably time to write your own views.</p>
<p>In this document we discuss how to activate, use, and customize Django’s admin
interface.</p>
<div class="section" id="s-overview">
<span id="overview"></span><h2>Overview<a class="headerlink" href="#overview" title="Permalink to this headline">¶</a></h2>
<p>The admin is enabled in the default project template used by
<a class="reference internal" href="../../django-admin.html#django-admin-startproject"><code class="xref std std-djadmin docutils literal notranslate"><span class="pre">startproject</span></code></a>.</p>
<p>For reference, here are the requirements:</p>
<ol class="arabic simple">
<li>Add <code class="docutils literal notranslate"><span class="pre">'django.contrib.admin'</span></code> to your <a class="reference internal" href="../../settings.html#std:setting-INSTALLED_APPS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">INSTALLED_APPS</span></code></a> setting.</li>
<li>The admin has four dependencies - <a class="reference internal" href="../../../topics/auth/index.html#module-django.contrib.auth" title="django.contrib.auth: Django's authentication framework."><code class="xref py py-mod docutils literal notranslate"><span class="pre">django.contrib.auth</span></code></a>,
<a class="reference internal" href="../contenttypes.html#module-django.contrib.contenttypes" title="django.contrib.contenttypes: Provides generic interface to installed models."><code class="xref py py-mod docutils literal notranslate"><span class="pre">django.contrib.contenttypes</span></code></a>,
<a class="reference internal" href="../messages.html#module-django.contrib.messages" title="django.contrib.messages: Provides cookie- and session-based temporary message storage."><code class="xref py py-mod docutils literal notranslate"><span class="pre">django.contrib.messages</span></code></a> and
<a class="reference internal" href="../../../topics/http/sessions.html#module-django.contrib.sessions" title="django.contrib.sessions: Provides session management for Django projects."><code class="xref py py-mod docutils literal notranslate"><span class="pre">django.contrib.sessions</span></code></a>.  If these applications are not
in your <a class="reference internal" href="../../settings.html#std:setting-INSTALLED_APPS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">INSTALLED_APPS</span></code></a> list, add them.</li>
<li>Add <code class="docutils literal notranslate"><span class="pre">django.contrib.auth.context_processors.auth</span></code> and
<code class="docutils literal notranslate"><span class="pre">django.contrib.messages.context_processors.messages</span></code> to
the <code class="docutils literal notranslate"><span class="pre">'context_processors'</span></code> option of the <code class="docutils literal notranslate"><span class="pre">DjangoTemplates</span></code> backend
defined in your <a class="reference internal" href="../../settings.html#std:setting-TEMPLATES"><code class="xref std std-setting docutils literal notranslate"><span class="pre">TEMPLATES</span></code></a> as well as
<a class="reference internal" href="../../middleware.html#django.contrib.auth.middleware.AuthenticationMiddleware" title="django.contrib.auth.middleware.AuthenticationMiddleware"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.contrib.auth.middleware.AuthenticationMiddleware</span></code></a> and
<a class="reference internal" href="../../middleware.html#django.contrib.messages.middleware.MessageMiddleware" title="django.contrib.messages.middleware.MessageMiddleware"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.contrib.messages.middleware.MessageMiddleware</span></code></a> to
<a class="reference internal" href="../../settings.html#std:setting-MIDDLEWARE"><code class="xref std std-setting docutils literal notranslate"><span class="pre">MIDDLEWARE</span></code></a>. These are all active by default, so you only need to
do this if you’ve manually tweaked the settings.</li>
<li>Determine which of your application’s models should be editable in the
admin interface.</li>
<li>For each of those models, optionally create a <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> class that
encapsulates the customized admin functionality and options for that
particular model.</li>
<li>Instantiate an <code class="docutils literal notranslate"><span class="pre">AdminSite</span></code> and tell it about each of your models and
<code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> classes.</li>
<li>Hook the <code class="docutils literal notranslate"><span class="pre">AdminSite</span></code> instance into your URLconf.</li>
</ol>
<p>After you’ve taken these steps, you’ll be able to use your Django admin site
by visiting the URL you hooked it into (<code class="docutils literal notranslate"><span class="pre">/admin/</span></code>, by default). If you need
to create a user to login with, you can use the <a class="reference internal" href="../../django-admin.html#django-admin-createsuperuser"><code class="xref std std-djadmin docutils literal notranslate"><span class="pre">createsuperuser</span></code></a>
command.</p>
<div class="section" id="s-other-topics">
<span id="other-topics"></span><h3>Other topics<a class="headerlink" href="#other-topics" title="Permalink to this headline">¶</a></h3>
<div class="toctree-wrapper compound">
<ul>
<li class="toctree-l1"><a class="reference internal" href="actions.html">Admin actions</a></li>
<li class="toctree-l1"><a class="reference internal" href="admindocs.html">The Django admin documentation generator</a></li>
<li class="toctree-l1"><a class="reference internal" href="javascript.html">JavaScript customizations in the admin</a></li>
</ul>
</div>
<div class="admonition seealso">
<p class="first admonition-title">See also</p>
<p>For information about serving the static files (images, JavaScript, and
CSS) associated with the admin in production, see <a class="reference internal" href="../../../howto/deployment/wsgi/modwsgi.html#serving-files"><span class="std std-ref">Serving files</span></a>.</p>
<p class="last">Having problems?  Try <a class="reference internal" href="../../../faq/admin.html"><span class="doc">FAQ: The admin</span></a>.</p>
</div>
</div>
</div>
<div class="section" id="s-modeladmin-objects">
<span id="modeladmin-objects"></span><h2><code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> objects<a class="headerlink" href="#modeladmin-objects" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="django.contrib.admin.ModelAdmin">
<em class="property">class </em><code class="descname">ModelAdmin</code><a class="reference internal" href="../../../_modules/django/contrib/admin/options.html#ModelAdmin"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.contrib.admin.ModelAdmin" title="Permalink to this definition">¶</a></dt>
<dd><p>The <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> class is the representation of a model in the admin
interface. Usually, these are stored in a file named <code class="docutils literal notranslate"><span class="pre">admin.py</span></code> in your
application. Let’s take a look at a very simple example of
the <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib</span> <span class="k">import</span> <span class="n">admin</span>
<span class="kn">from</span> <span class="nn">myproject.myapp.models</span> <span class="k">import</span> <span class="n">Author</span>

<span class="k">class</span> <span class="nc">AuthorAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="k">pass</span>
<span class="n">admin</span><span class="o">.</span><span class="n">site</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">Author</span><span class="p">,</span> <span class="n">AuthorAdmin</span><span class="p">)</span>
</pre></div>
</div>
<div class="admonition-do-you-need-a-modeladmin-object-at-all admonition">
<p class="first admonition-title">Do you need a <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> object at all?</p>
<p>In the preceding example, the <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> class doesn’t define any
custom values (yet). As a result, the default admin interface will be
provided. If you are happy with the default admin interface, you don’t
need to define a <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> object at all – you can register the
model class without providing a <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> description. The
preceding example could be simplified to:</p>
<div class="last highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib</span> <span class="k">import</span> <span class="n">admin</span>
<span class="kn">from</span> <span class="nn">myproject.myapp.models</span> <span class="k">import</span> <span class="n">Author</span>

<span class="n">admin</span><span class="o">.</span><span class="n">site</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">Author</span><span class="p">)</span>
</pre></div>
</div>
</div>
</dd></dl>

<div class="section" id="s-the-register-decorator">
<span id="the-register-decorator"></span><h3>The <code class="docutils literal notranslate"><span class="pre">register</span></code> decorator<a class="headerlink" href="#the-register-decorator" title="Permalink to this headline">¶</a></h3>
<dl class="function">
<dt id="django.contrib.admin.register">
<code class="descname">register</code>(<em>*models</em>, <em>site=django.admin.sites.site</em>)<a class="reference internal" href="../../../_modules/django/contrib/admin/decorators.html#register"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.contrib.admin.register" title="Permalink to this definition">¶</a></dt>
<dd><p>There is also a decorator for registering your <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> classes:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib</span> <span class="k">import</span> <span class="n">admin</span>
<span class="kn">from</span> <span class="nn">.models</span> <span class="k">import</span> <span class="n">Author</span>

<span class="nd">@admin</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">Author</span><span class="p">)</span>
<span class="k">class</span> <span class="nc">AuthorAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="k">pass</span>
</pre></div>
</div>
<p>It is given one or more model classes to register with the <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code>
and an optional keyword argument <code class="docutils literal notranslate"><span class="pre">site</span></code> if you are not using the default
<code class="docutils literal notranslate"><span class="pre">AdminSite</span></code>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib</span> <span class="k">import</span> <span class="n">admin</span>
<span class="kn">from</span> <span class="nn">.models</span> <span class="k">import</span> <span class="n">Author</span><span class="p">,</span> <span class="n">Reader</span><span class="p">,</span> <span class="n">Editor</span>
<span class="kn">from</span> <span class="nn">myproject.admin_site</span> <span class="k">import</span> <span class="n">custom_admin_site</span>

<span class="nd">@admin</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">Author</span><span class="p">,</span> <span class="n">Reader</span><span class="p">,</span> <span class="n">Editor</span><span class="p">,</span> <span class="n">site</span><span class="o">=</span><span class="n">custom_admin_site</span><span class="p">)</span>
<span class="k">class</span> <span class="nc">PersonAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="k">pass</span>
</pre></div>
</div>
<p>You can’t use this decorator if you have to reference your model admin
class in its <code class="docutils literal notranslate"><span class="pre">__init__()</span></code> method, e.g.
<code class="docutils literal notranslate"><span class="pre">super(PersonAdmin,</span> <span class="pre">self).__init__(*args,</span> <span class="pre">**kwargs)</span></code>. If you are using
Python 3 and don’t have to worry about supporting Python 2, you can
use <code class="docutils literal notranslate"><span class="pre">super().__init__(*args,</span> <span class="pre">**kwargs)</span></code> . Otherwise, you’ll have to use
<code class="docutils literal notranslate"><span class="pre">admin.site.register()</span></code> instead of this decorator.</p>
</dd></dl>

</div>
<div class="section" id="s-discovery-of-admin-files">
<span id="discovery-of-admin-files"></span><h3>Discovery of admin files<a class="headerlink" href="#discovery-of-admin-files" title="Permalink to this headline">¶</a></h3>
<p>When you put <code class="docutils literal notranslate"><span class="pre">'django.contrib.admin'</span></code> in your <a class="reference internal" href="../../settings.html#std:setting-INSTALLED_APPS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">INSTALLED_APPS</span></code></a>
setting, Django automatically looks for an <code class="docutils literal notranslate"><span class="pre">admin</span></code> module in each
application and imports it.</p>
<dl class="class">
<dt id="django.contrib.admin.apps.AdminConfig">
<em class="property">class </em><code class="descclassname">apps.</code><code class="descname">AdminConfig</code><a class="headerlink" href="#django.contrib.admin.apps.AdminConfig" title="Permalink to this definition">¶</a></dt>
<dd><p>This is the default <a class="reference internal" href="../../applications.html#django.apps.AppConfig" title="django.apps.AppConfig"><code class="xref py py-class docutils literal notranslate"><span class="pre">AppConfig</span></code></a> class for the admin.
It calls <a class="reference internal" href="#django.contrib.admin.autodiscover" title="django.contrib.admin.autodiscover"><code class="xref py py-func docutils literal notranslate"><span class="pre">autodiscover()</span></code></a> when Django starts.</p>
</dd></dl>

<dl class="class">
<dt id="django.contrib.admin.apps.SimpleAdminConfig">
<em class="property">class </em><code class="descclassname">apps.</code><code class="descname">SimpleAdminConfig</code><a class="headerlink" href="#django.contrib.admin.apps.SimpleAdminConfig" title="Permalink to this definition">¶</a></dt>
<dd><p>This class works like <a class="reference internal" href="#django.contrib.admin.apps.AdminConfig" title="django.contrib.admin.apps.AdminConfig"><code class="xref py py-class docutils literal notranslate"><span class="pre">AdminConfig</span></code></a>,
except it doesn’t call <a class="reference internal" href="#django.contrib.admin.autodiscover" title="django.contrib.admin.autodiscover"><code class="xref py py-func docutils literal notranslate"><span class="pre">autodiscover()</span></code></a>.</p>
</dd></dl>

<dl class="function">
<dt id="django.contrib.admin.autodiscover">
<code class="descname">autodiscover</code>()<a class="reference internal" href="../../../_modules/django/contrib/admin.html#autodiscover"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.contrib.admin.autodiscover" title="Permalink to this definition">¶</a></dt>
<dd><p>This function attempts to import an <code class="docutils literal notranslate"><span class="pre">admin</span></code> module in each installed
application. Such modules are expected to register models with the admin.</p>
<p>Typically you won’t need to call this function directly as
<a class="reference internal" href="#django.contrib.admin.apps.AdminConfig" title="django.contrib.admin.apps.AdminConfig"><code class="xref py py-class docutils literal notranslate"><span class="pre">AdminConfig</span></code></a> calls it when Django starts.</p>
</dd></dl>

<p>If you are using a custom <code class="docutils literal notranslate"><span class="pre">AdminSite</span></code>, it is common to import all of the
<code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> subclasses into your code and register them to the custom
<code class="docutils literal notranslate"><span class="pre">AdminSite</span></code>. In that case, in order to disable auto-discovery, you should
put <code class="docutils literal notranslate"><span class="pre">'django.contrib.admin.apps.SimpleAdminConfig'</span></code> instead of
<code class="docutils literal notranslate"><span class="pre">'django.contrib.admin'</span></code> in your <a class="reference internal" href="../../settings.html#std:setting-INSTALLED_APPS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">INSTALLED_APPS</span></code></a> setting.</p>
</div>
<div class="section" id="s-modeladmin-options">
<span id="modeladmin-options"></span><h3><code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> options<a class="headerlink" href="#modeladmin-options" title="Permalink to this headline">¶</a></h3>
<p>The <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> is very flexible. It has several options for dealing with
customizing the interface. All options are defined on the <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code>
subclass:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib</span> <span class="k">import</span> <span class="n">admin</span>

<span class="k">class</span> <span class="nc">AuthorAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">date_hierarchy</span> <span class="o">=</span> <span class="s1">&#39;pub_date&#39;</span>
</pre></div>
</div>
<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.actions">
<code class="descclassname">ModelAdmin.</code><code class="descname">actions</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.actions" title="Permalink to this definition">¶</a></dt>
<dd><p>A list of actions to make available on the change list page. See
<a class="reference internal" href="actions.html"><span class="doc">Admin actions</span></a> for details.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.actions_on_top">
<code class="descclassname">ModelAdmin.</code><code class="descname">actions_on_top</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.actions_on_top" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.actions_on_bottom">
<code class="descclassname">ModelAdmin.</code><code class="descname">actions_on_bottom</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.actions_on_bottom" title="Permalink to this definition">¶</a></dt>
<dd><p>Controls where on the page the actions bar appears. By default, the admin
changelist displays actions at the top of the page (<code class="docutils literal notranslate"><span class="pre">actions_on_top</span> <span class="pre">=</span> <span class="pre">True;</span>
<span class="pre">actions_on_bottom</span> <span class="pre">=</span> <span class="pre">False</span></code>).</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.actions_selection_counter">
<code class="descclassname">ModelAdmin.</code><code class="descname">actions_selection_counter</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.actions_selection_counter" title="Permalink to this definition">¶</a></dt>
<dd><p>Controls whether a selection counter is displayed next to the action dropdown.
By default, the admin changelist will display it
(<code class="docutils literal notranslate"><span class="pre">actions_selection_counter</span> <span class="pre">=</span> <span class="pre">True</span></code>).</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.date_hierarchy">
<code class="descclassname">ModelAdmin.</code><code class="descname">date_hierarchy</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.date_hierarchy" title="Permalink to this definition">¶</a></dt>
<dd><p>Set <code class="docutils literal notranslate"><span class="pre">date_hierarchy</span></code> to the name of a <code class="docutils literal notranslate"><span class="pre">DateField</span></code> or <code class="docutils literal notranslate"><span class="pre">DateTimeField</span></code>
in your model, and the change list page will include a date-based drilldown
navigation by that field.</p>
<p>Example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">date_hierarchy</span> <span class="o">=</span> <span class="s1">&#39;pub_date&#39;</span>
</pre></div>
</div>
<p>You can also specify a field on a related model using the <code class="docutils literal notranslate"><span class="pre">__</span></code> lookup,
for example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">date_hierarchy</span> <span class="o">=</span> <span class="s1">&#39;author__pub_date&#39;</span>
</pre></div>
</div>
<p>This will intelligently populate itself based on available data,
e.g. if all the dates are in one month, it’ll show the day-level
drill-down only.</p>
<div class="versionchanged">
<span class="title">Changed in Django 1.11:</span> <p>The ability to reference fields on related models was added.</p>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last"><code class="docutils literal notranslate"><span class="pre">date_hierarchy</span></code> uses <a class="reference internal" href="../../models/querysets.html#django.db.models.query.QuerySet.datetimes" title="django.db.models.query.QuerySet.datetimes"><code class="xref py py-meth docutils literal notranslate"><span class="pre">QuerySet.datetimes()</span></code></a> internally. Please refer
to its documentation for some caveats when time zone support is
enabled (<a class="reference internal" href="../../settings.html#std:setting-USE_TZ"><code class="xref std std-setting docutils literal notranslate"><span class="pre">USE_TZ</span> <span class="pre">=</span> <span class="pre">True</span></code></a>).</p>
</div>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.empty_value_display">
<code class="descclassname">ModelAdmin.</code><code class="descname">empty_value_display</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.empty_value_display" title="Permalink to this definition">¶</a></dt>
<dd><p>This attribute overrides the default display value for record’s fields that
are empty (<code class="docutils literal notranslate"><span class="pre">None</span></code>, empty string, etc.). The default value is <code class="docutils literal notranslate"><span class="pre">-</span></code> (a
dash). For example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib</span> <span class="k">import</span> <span class="n">admin</span>

<span class="k">class</span> <span class="nc">AuthorAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">empty_value_display</span> <span class="o">=</span> <span class="s1">&#39;-empty-&#39;</span>
</pre></div>
</div>
<p>You can also override <code class="docutils literal notranslate"><span class="pre">empty_value_display</span></code> for all admin pages with
<a class="reference internal" href="#django.contrib.admin.AdminSite.empty_value_display" title="django.contrib.admin.AdminSite.empty_value_display"><code class="xref py py-attr docutils literal notranslate"><span class="pre">AdminSite.empty_value_display</span></code></a>, or for specific fields like this:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib</span> <span class="k">import</span> <span class="n">admin</span>

<span class="k">class</span> <span class="nc">AuthorAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">fields</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">,</span> <span class="s1">&#39;title&#39;</span><span class="p">,</span> <span class="s1">&#39;view_birth_date&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">view_birth_date</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">obj</span><span class="o">.</span><span class="n">birth_date</span>

    <span class="n">view_birth_date</span><span class="o">.</span><span class="n">empty_value_display</span> <span class="o">=</span> <span class="s1">&#39;???&#39;</span>
</pre></div>
</div>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.exclude">
<code class="descclassname">ModelAdmin.</code><code class="descname">exclude</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.exclude" title="Permalink to this definition">¶</a></dt>
<dd><p>This attribute, if given, should be a list of field names to exclude from
the form.</p>
<p>For example, let’s consider the following model:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.db</span> <span class="k">import</span> <span class="n">models</span>

<span class="k">class</span> <span class="nc">Author</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>
    <span class="n">title</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
    <span class="n">birth_date</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">DateField</span><span class="p">(</span><span class="n">blank</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">null</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
</pre></div>
</div>
<p>If you want a form for the <code class="docutils literal notranslate"><span class="pre">Author</span></code> model that includes only the <code class="docutils literal notranslate"><span class="pre">name</span></code>
and <code class="docutils literal notranslate"><span class="pre">title</span></code> fields, you would specify <code class="docutils literal notranslate"><span class="pre">fields</span></code> or <code class="docutils literal notranslate"><span class="pre">exclude</span></code> like
this:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib</span> <span class="k">import</span> <span class="n">admin</span>

<span class="k">class</span> <span class="nc">AuthorAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">fields</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">,</span> <span class="s1">&#39;title&#39;</span><span class="p">)</span>

<span class="k">class</span> <span class="nc">AuthorAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">exclude</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;birth_date&#39;</span><span class="p">,)</span>
</pre></div>
</div>
<p>Since the Author model only has three fields, <code class="docutils literal notranslate"><span class="pre">name</span></code>, <code class="docutils literal notranslate"><span class="pre">title</span></code>, and
<code class="docutils literal notranslate"><span class="pre">birth_date</span></code>, the forms resulting from the above declarations will
contain exactly the same fields.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.fields">
<code class="descclassname">ModelAdmin.</code><code class="descname">fields</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.fields" title="Permalink to this definition">¶</a></dt>
<dd><p>Use the <code class="docutils literal notranslate"><span class="pre">fields</span></code> option to make simple layout changes in the forms on
the “add” and “change” pages such as showing only a subset of available
fields, modifying their order, or grouping them into rows. For example, you
could define a simpler version of the admin form for the
<a class="reference internal" href="../flatpages.html#django.contrib.flatpages.models.FlatPage" title="django.contrib.flatpages.models.FlatPage"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.contrib.flatpages.models.FlatPage</span></code></a> model as follows:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">FlatPageAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">fields</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;url&#39;</span><span class="p">,</span> <span class="s1">&#39;title&#39;</span><span class="p">,</span> <span class="s1">&#39;content&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>In the above example, only the fields <code class="docutils literal notranslate"><span class="pre">url</span></code>, <code class="docutils literal notranslate"><span class="pre">title</span></code> and <code class="docutils literal notranslate"><span class="pre">content</span></code>
will be displayed, sequentially, in the form. <code class="docutils literal notranslate"><span class="pre">fields</span></code> can contain
values defined in <a class="reference internal" href="#django.contrib.admin.ModelAdmin.readonly_fields" title="django.contrib.admin.ModelAdmin.readonly_fields"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ModelAdmin.readonly_fields</span></code></a> to be displayed as
read-only.</p>
<p>For more complex layout needs, see the <a class="reference internal" href="#django.contrib.admin.ModelAdmin.fieldsets" title="django.contrib.admin.ModelAdmin.fieldsets"><code class="xref py py-attr docutils literal notranslate"><span class="pre">fieldsets</span></code></a> option.</p>
<p>The <code class="docutils literal notranslate"><span class="pre">fields</span></code> option, unlike <a class="reference internal" href="#django.contrib.admin.ModelAdmin.list_display" title="django.contrib.admin.ModelAdmin.list_display"><code class="xref py py-attr docutils literal notranslate"><span class="pre">list_display</span></code></a>, may only
contain names of fields on the model or the form specified by
<a class="reference internal" href="#django.contrib.admin.ModelAdmin.form" title="django.contrib.admin.ModelAdmin.form"><code class="xref py py-attr docutils literal notranslate"><span class="pre">form</span></code></a>. It may contain callables only if they are listed
in <a class="reference internal" href="#django.contrib.admin.ModelAdmin.readonly_fields" title="django.contrib.admin.ModelAdmin.readonly_fields"><code class="xref py py-attr docutils literal notranslate"><span class="pre">readonly_fields</span></code></a>.</p>
<p>To display multiple fields on the same line, wrap those fields in their own
tuple. In this example, the <code class="docutils literal notranslate"><span class="pre">url</span></code> and <code class="docutils literal notranslate"><span class="pre">title</span></code> fields will display on the
same line and the <code class="docutils literal notranslate"><span class="pre">content</span></code> field will be displayed below them on its
own line:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">FlatPageAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">fields</span> <span class="o">=</span> <span class="p">((</span><span class="s1">&#39;url&#39;</span><span class="p">,</span> <span class="s1">&#39;title&#39;</span><span class="p">),</span> <span class="s1">&#39;content&#39;</span><span class="p">)</span>
</pre></div>
</div>
<div class="admonition-note admonition">
<p class="first admonition-title">Note</p>
<p class="last">This <code class="docutils literal notranslate"><span class="pre">fields</span></code> option should not be confused with the <code class="docutils literal notranslate"><span class="pre">fields</span></code>
dictionary key that is within the <a class="reference internal" href="#django.contrib.admin.ModelAdmin.fieldsets" title="django.contrib.admin.ModelAdmin.fieldsets"><code class="xref py py-attr docutils literal notranslate"><span class="pre">fieldsets</span></code></a> option,
as described in the next section.</p>
</div>
<p>If neither <code class="docutils literal notranslate"><span class="pre">fields</span></code> nor <a class="reference internal" href="#django.contrib.admin.ModelAdmin.fieldsets" title="django.contrib.admin.ModelAdmin.fieldsets"><code class="xref py py-attr docutils literal notranslate"><span class="pre">fieldsets</span></code></a> options are present,
Django will default to displaying each field that isn’t an <code class="docutils literal notranslate"><span class="pre">AutoField</span></code> and
has <code class="docutils literal notranslate"><span class="pre">editable=True</span></code>, in a single fieldset, in the same order as the fields
are defined in the model.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.fieldsets">
<code class="descclassname">ModelAdmin.</code><code class="descname">fieldsets</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.fieldsets" title="Permalink to this definition">¶</a></dt>
<dd><p>Set <code class="docutils literal notranslate"><span class="pre">fieldsets</span></code> to control the layout of admin “add” and “change” pages.</p>
<p><code class="docutils literal notranslate"><span class="pre">fieldsets</span></code> is a list of two-tuples, in which each two-tuple represents a
<code class="docutils literal notranslate"><span class="pre">&lt;fieldset&gt;</span></code> on the admin form page. (A <code class="docutils literal notranslate"><span class="pre">&lt;fieldset&gt;</span></code> is a “section” of
the form.)</p>
<p>The two-tuples are in the format <code class="docutils literal notranslate"><span class="pre">(name,</span> <span class="pre">field_options)</span></code>, where <code class="docutils literal notranslate"><span class="pre">name</span></code>
is a string representing the title of the fieldset and <code class="docutils literal notranslate"><span class="pre">field_options</span></code> is
a dictionary of information about the fieldset, including a list of fields
to be displayed in it.</p>
<p>A full example, taken from the
<a class="reference internal" href="../flatpages.html#django.contrib.flatpages.models.FlatPage" title="django.contrib.flatpages.models.FlatPage"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.contrib.flatpages.models.FlatPage</span></code></a> model:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib</span> <span class="k">import</span> <span class="n">admin</span>

<span class="k">class</span> <span class="nc">FlatPageAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">fieldsets</span> <span class="o">=</span> <span class="p">(</span>
        <span class="p">(</span><span class="kc">None</span><span class="p">,</span> <span class="p">{</span>
            <span class="s1">&#39;fields&#39;</span><span class="p">:</span> <span class="p">(</span><span class="s1">&#39;url&#39;</span><span class="p">,</span> <span class="s1">&#39;title&#39;</span><span class="p">,</span> <span class="s1">&#39;content&#39;</span><span class="p">,</span> <span class="s1">&#39;sites&#39;</span><span class="p">)</span>
        <span class="p">}),</span>
        <span class="p">(</span><span class="s1">&#39;Advanced options&#39;</span><span class="p">,</span> <span class="p">{</span>
            <span class="s1">&#39;classes&#39;</span><span class="p">:</span> <span class="p">(</span><span class="s1">&#39;collapse&#39;</span><span class="p">,),</span>
            <span class="s1">&#39;fields&#39;</span><span class="p">:</span> <span class="p">(</span><span class="s1">&#39;registration_required&#39;</span><span class="p">,</span> <span class="s1">&#39;template_name&#39;</span><span class="p">),</span>
        <span class="p">}),</span>
    <span class="p">)</span>
</pre></div>
</div>
<p>This results in an admin page that looks like:</p>
<img alt="../../../_images/fieldsets.png" src="../../../_images/fieldsets.png" />
<p>If neither <code class="docutils literal notranslate"><span class="pre">fieldsets</span></code> nor <a class="reference internal" href="#django.contrib.admin.ModelAdmin.fields" title="django.contrib.admin.ModelAdmin.fields"><code class="xref py py-attr docutils literal notranslate"><span class="pre">fields</span></code></a> options are present,
Django will default to displaying each field that isn’t an <code class="docutils literal notranslate"><span class="pre">AutoField</span></code> and
has <code class="docutils literal notranslate"><span class="pre">editable=True</span></code>, in a single fieldset, in the same order as the fields
are defined in the model.</p>
<p>The <code class="docutils literal notranslate"><span class="pre">field_options</span></code> dictionary can have the following keys:</p>
<ul>
<li><dl class="first docutils">
<dt><code class="docutils literal notranslate"><span class="pre">fields</span></code></dt>
<dd><p class="first">A tuple of field names to display in this fieldset. This key is
required.</p>
<p>Example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="p">{</span>
<span class="s1">&#39;fields&#39;</span><span class="p">:</span> <span class="p">(</span><span class="s1">&#39;first_name&#39;</span><span class="p">,</span> <span class="s1">&#39;last_name&#39;</span><span class="p">,</span> <span class="s1">&#39;address&#39;</span><span class="p">,</span> <span class="s1">&#39;city&#39;</span><span class="p">,</span> <span class="s1">&#39;state&#39;</span><span class="p">),</span>
<span class="p">}</span>
</pre></div>
</div>
<p>As with the <a class="reference internal" href="#django.contrib.admin.ModelAdmin.fields" title="django.contrib.admin.ModelAdmin.fields"><code class="xref py py-attr docutils literal notranslate"><span class="pre">fields</span></code></a> option, to display multiple
fields on the same line, wrap those fields in their own tuple. In this
example, the <code class="docutils literal notranslate"><span class="pre">first_name</span></code> and <code class="docutils literal notranslate"><span class="pre">last_name</span></code> fields will display on
the same line:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="p">{</span>
<span class="s1">&#39;fields&#39;</span><span class="p">:</span> <span class="p">((</span><span class="s1">&#39;first_name&#39;</span><span class="p">,</span> <span class="s1">&#39;last_name&#39;</span><span class="p">),</span> <span class="s1">&#39;address&#39;</span><span class="p">,</span> <span class="s1">&#39;city&#39;</span><span class="p">,</span> <span class="s1">&#39;state&#39;</span><span class="p">),</span>
<span class="p">}</span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">fields</span></code> can contain values defined in
<a class="reference internal" href="#django.contrib.admin.ModelAdmin.readonly_fields" title="django.contrib.admin.ModelAdmin.readonly_fields"><code class="xref py py-attr docutils literal notranslate"><span class="pre">readonly_fields</span></code></a> to be displayed as read-only.</p>
<p class="last">If you add the name of a callable to <code class="docutils literal notranslate"><span class="pre">fields</span></code>, the same rule applies
as with the <a class="reference internal" href="#django.contrib.admin.ModelAdmin.fields" title="django.contrib.admin.ModelAdmin.fields"><code class="xref py py-attr docutils literal notranslate"><span class="pre">fields</span></code></a> option: the callable must be
listed in <a class="reference internal" href="#django.contrib.admin.ModelAdmin.readonly_fields" title="django.contrib.admin.ModelAdmin.readonly_fields"><code class="xref py py-attr docutils literal notranslate"><span class="pre">readonly_fields</span></code></a>.</p>
</dd>
</dl>
</li>
<li><dl class="first docutils">
<dt><code class="docutils literal notranslate"><span class="pre">classes</span></code></dt>
<dd><p class="first">A list or tuple containing extra CSS classes to apply to the fieldset.</p>
<p>Example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="p">{</span>
<span class="s1">&#39;classes&#39;</span><span class="p">:</span> <span class="p">(</span><span class="s1">&#39;wide&#39;</span><span class="p">,</span> <span class="s1">&#39;extrapretty&#39;</span><span class="p">),</span>
<span class="p">}</span>
</pre></div>
</div>
<p class="last">Two useful classes defined by the default admin site stylesheet are
<code class="docutils literal notranslate"><span class="pre">collapse</span></code> and <code class="docutils literal notranslate"><span class="pre">wide</span></code>. Fieldsets with the <code class="docutils literal notranslate"><span class="pre">collapse</span></code> style
will be initially collapsed in the admin and replaced with a small
“click to expand” link. Fieldsets with the <code class="docutils literal notranslate"><span class="pre">wide</span></code> style will be
given extra horizontal space.</p>
</dd>
</dl>
</li>
<li><dl class="first docutils">
<dt><code class="docutils literal notranslate"><span class="pre">description</span></code></dt>
<dd><p class="first">A string of optional extra text to be displayed at the top of each
fieldset, under the heading of the fieldset. This string is not
rendered for <a class="reference internal" href="#django.contrib.admin.TabularInline" title="django.contrib.admin.TabularInline"><code class="xref py py-class docutils literal notranslate"><span class="pre">TabularInline</span></code></a> due to its
layout.</p>
<p class="last">Note that this value is <em>not</em> HTML-escaped when it’s displayed in
the admin interface. This lets you include HTML if you so desire.
Alternatively you can use plain text and
<code class="docutils literal notranslate"><span class="pre">django.utils.html.escape()</span></code> to escape any HTML special
characters.</p>
</dd>
</dl>
</li>
</ul>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.filter_horizontal">
<code class="descclassname">ModelAdmin.</code><code class="descname">filter_horizontal</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.filter_horizontal" title="Permalink to this definition">¶</a></dt>
<dd><p>By default, a <a class="reference internal" href="../../models/fields.html#django.db.models.ManyToManyField" title="django.db.models.ManyToManyField"><code class="xref py py-class docutils literal notranslate"><span class="pre">ManyToManyField</span></code></a> is displayed in
the admin site with a <code class="docutils literal notranslate"><span class="pre">&lt;select</span> <span class="pre">multiple&gt;</span></code>. However, multiple-select boxes
can be difficult to use when selecting many items. Adding a
<a class="reference internal" href="../../models/fields.html#django.db.models.ManyToManyField" title="django.db.models.ManyToManyField"><code class="xref py py-class docutils literal notranslate"><span class="pre">ManyToManyField</span></code></a> to this list will instead use
a nifty unobtrusive JavaScript “filter” interface that allows searching
within the options. The unselected and selected options appear in two boxes
side by side. See <a class="reference internal" href="#django.contrib.admin.ModelAdmin.filter_vertical" title="django.contrib.admin.ModelAdmin.filter_vertical"><code class="xref py py-attr docutils literal notranslate"><span class="pre">filter_vertical</span></code></a> to use a vertical
interface.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.filter_vertical">
<code class="descclassname">ModelAdmin.</code><code class="descname">filter_vertical</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.filter_vertical" title="Permalink to this definition">¶</a></dt>
<dd><p>Same as <a class="reference internal" href="#django.contrib.admin.ModelAdmin.filter_horizontal" title="django.contrib.admin.ModelAdmin.filter_horizontal"><code class="xref py py-attr docutils literal notranslate"><span class="pre">filter_horizontal</span></code></a>, but uses a vertical display
of the filter interface with the box of unselected options appearing above
the box of selected options.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.form">
<code class="descclassname">ModelAdmin.</code><code class="descname">form</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.form" title="Permalink to this definition">¶</a></dt>
<dd><p>By default a <code class="docutils literal notranslate"><span class="pre">ModelForm</span></code> is dynamically created for your model. It is
used to create the form presented on both the add/change pages. You can
easily provide your own <code class="docutils literal notranslate"><span class="pre">ModelForm</span></code> to override any default form behavior
on the add/change pages. Alternatively, you can customize the default
form rather than specifying an entirely new one by using the
<a class="reference internal" href="#django.contrib.admin.ModelAdmin.get_form" title="django.contrib.admin.ModelAdmin.get_form"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ModelAdmin.get_form()</span></code></a> method.</p>
<p>For an example see the section <a class="reference internal" href="#admin-custom-validation"><span class="std std-ref">Adding custom validation to the admin</span></a>.</p>
<div class="admonition-note admonition">
<p class="first admonition-title">Note</p>
<p>If you define the <code class="docutils literal notranslate"><span class="pre">Meta.model</span></code> attribute on a
<a class="reference internal" href="../../../topics/forms/modelforms.html#django.forms.ModelForm" title="django.forms.ModelForm"><code class="xref py py-class docutils literal notranslate"><span class="pre">ModelForm</span></code></a>, you must also define the
<code class="docutils literal notranslate"><span class="pre">Meta.fields</span></code> attribute (or the <code class="docutils literal notranslate"><span class="pre">Meta.exclude</span></code> attribute). However,
since the admin has its own way of defining fields, the <code class="docutils literal notranslate"><span class="pre">Meta.fields</span></code>
attribute will be ignored.</p>
<p class="last">If the <code class="docutils literal notranslate"><span class="pre">ModelForm</span></code> is only going to be used for the admin, the easiest
solution is to omit the <code class="docutils literal notranslate"><span class="pre">Meta.model</span></code> attribute, since <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code>
will provide the correct model to use. Alternatively, you can set
<code class="docutils literal notranslate"><span class="pre">fields</span> <span class="pre">=</span> <span class="pre">[]</span></code> in the <code class="docutils literal notranslate"><span class="pre">Meta</span></code> class to satisfy the validation on the
<code class="docutils literal notranslate"><span class="pre">ModelForm</span></code>.</p>
</div>
<div class="admonition-note admonition">
<p class="first admonition-title">Note</p>
<p>If your <code class="docutils literal notranslate"><span class="pre">ModelForm</span></code> and <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> both define an <code class="docutils literal notranslate"><span class="pre">exclude</span></code>
option then <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> takes precedence:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django</span> <span class="k">import</span> <span class="n">forms</span>
<span class="kn">from</span> <span class="nn">django.contrib</span> <span class="k">import</span> <span class="n">admin</span>
<span class="kn">from</span> <span class="nn">myapp.models</span> <span class="k">import</span> <span class="n">Person</span>

<span class="k">class</span> <span class="nc">PersonForm</span><span class="p">(</span><span class="n">forms</span><span class="o">.</span><span class="n">ModelForm</span><span class="p">):</span>

    <span class="k">class</span> <span class="nc">Meta</span><span class="p">:</span>
        <span class="n">model</span> <span class="o">=</span> <span class="n">Person</span>
        <span class="n">exclude</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;name&#39;</span><span class="p">]</span>

<span class="k">class</span> <span class="nc">PersonAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">exclude</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;age&#39;</span><span class="p">]</span>
    <span class="n">form</span> <span class="o">=</span> <span class="n">PersonForm</span>
</pre></div>
</div>
<p class="last">In the above example, the “age” field will be excluded but the “name”
field will be included in the generated form.</p>
</div>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.formfield_overrides">
<code class="descclassname">ModelAdmin.</code><code class="descname">formfield_overrides</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.formfield_overrides" title="Permalink to this definition">¶</a></dt>
<dd><p>This provides a quick-and-dirty way to override some of the
<a class="reference internal" href="../../forms/fields.html#django.forms.Field" title="django.forms.Field"><code class="xref py py-class docutils literal notranslate"><span class="pre">Field</span></code></a> options for use in the admin.
<code class="docutils literal notranslate"><span class="pre">formfield_overrides</span></code> is a dictionary mapping a field class to a dict of
arguments to pass to the field at construction time.</p>
<p>Since that’s a bit abstract, let’s look at a concrete example. The most
common use of <code class="docutils literal notranslate"><span class="pre">formfield_overrides</span></code> is to add a custom widget for a
certain type of field. So, imagine we’ve written a <code class="docutils literal notranslate"><span class="pre">RichTextEditorWidget</span></code>
that we’d like to use for large text fields instead of the default
<code class="docutils literal notranslate"><span class="pre">&lt;textarea&gt;</span></code>. Here’s how we’d do that:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.db</span> <span class="k">import</span> <span class="n">models</span>
<span class="kn">from</span> <span class="nn">django.contrib</span> <span class="k">import</span> <span class="n">admin</span>

<span class="c1"># Import our custom widget and our model from where they&#39;re defined</span>
<span class="kn">from</span> <span class="nn">myapp.widgets</span> <span class="k">import</span> <span class="n">RichTextEditorWidget</span>
<span class="kn">from</span> <span class="nn">myapp.models</span> <span class="k">import</span> <span class="n">MyModel</span>

<span class="k">class</span> <span class="nc">MyModelAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">formfield_overrides</span> <span class="o">=</span> <span class="p">{</span>
        <span class="n">models</span><span class="o">.</span><span class="n">TextField</span><span class="p">:</span> <span class="p">{</span><span class="s1">&#39;widget&#39;</span><span class="p">:</span> <span class="n">RichTextEditorWidget</span><span class="p">},</span>
    <span class="p">}</span>
</pre></div>
</div>
<p>Note that the key in the dictionary is the actual field class, <em>not</em> a
string. The value is another dictionary; these arguments will be passed to
the form field’s <code class="docutils literal notranslate"><span class="pre">__init__()</span></code> method. See <a class="reference internal" href="../../forms/api.html"><span class="doc">The Forms API</span></a> for
details.</p>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p>If you want to use a custom widget with a relation field (i.e.
<a class="reference internal" href="../../models/fields.html#django.db.models.ForeignKey" title="django.db.models.ForeignKey"><code class="xref py py-class docutils literal notranslate"><span class="pre">ForeignKey</span></code></a> or
<a class="reference internal" href="../../models/fields.html#django.db.models.ManyToManyField" title="django.db.models.ManyToManyField"><code class="xref py py-class docutils literal notranslate"><span class="pre">ManyToManyField</span></code></a>), make sure you haven’t
included that field’s name in <code class="docutils literal notranslate"><span class="pre">raw_id_fields</span></code> or <code class="docutils literal notranslate"><span class="pre">radio_fields</span></code>.</p>
<p class="last"><code class="docutils literal notranslate"><span class="pre">formfield_overrides</span></code> won’t let you change the widget on relation
fields that have <code class="docutils literal notranslate"><span class="pre">raw_id_fields</span></code> or <code class="docutils literal notranslate"><span class="pre">radio_fields</span></code> set. That’s
because <code class="docutils literal notranslate"><span class="pre">raw_id_fields</span></code> and <code class="docutils literal notranslate"><span class="pre">radio_fields</span></code> imply custom widgets of
their own.</p>
</div>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.inlines">
<code class="descclassname">ModelAdmin.</code><code class="descname">inlines</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.inlines" title="Permalink to this definition">¶</a></dt>
<dd><p>See <a class="reference internal" href="#django.contrib.admin.InlineModelAdmin" title="django.contrib.admin.InlineModelAdmin"><code class="xref py py-class docutils literal notranslate"><span class="pre">InlineModelAdmin</span></code></a> objects below as well as
<a class="reference internal" href="#django.contrib.admin.ModelAdmin.get_formsets_with_inlines" title="django.contrib.admin.ModelAdmin.get_formsets_with_inlines"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ModelAdmin.get_formsets_with_inlines()</span></code></a>.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.list_display">
<code class="descclassname">ModelAdmin.</code><code class="descname">list_display</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.list_display" title="Permalink to this definition">¶</a></dt>
<dd><p>Set <code class="docutils literal notranslate"><span class="pre">list_display</span></code> to control which fields are displayed on the change
list page of the admin.</p>
<p>Example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">list_display</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;first_name&#39;</span><span class="p">,</span> <span class="s1">&#39;last_name&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>If you don’t set <code class="docutils literal notranslate"><span class="pre">list_display</span></code>, the admin site will display a single
column that displays the <code class="docutils literal notranslate"><span class="pre">__str__()</span></code> (<code class="docutils literal notranslate"><span class="pre">__unicode__()</span></code> on Python 2)
representation of each object.</p>
<p>You have four possible values that can be used in <code class="docutils literal notranslate"><span class="pre">list_display</span></code>:</p>
<ul>
<li><p class="first">A field of the model. For example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">PersonAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">list_display</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;first_name&#39;</span><span class="p">,</span> <span class="s1">&#39;last_name&#39;</span><span class="p">)</span>
</pre></div>
</div>
</li>
<li><p class="first">A callable that accepts one parameter for the model instance. For
example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">upper_case_name</span><span class="p">(</span><span class="n">obj</span><span class="p">):</span>
    <span class="k">return</span> <span class="p">(</span><span class="s2">&quot;</span><span class="si">%s</span><span class="s2"> </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">obj</span><span class="o">.</span><span class="n">first_name</span><span class="p">,</span> <span class="n">obj</span><span class="o">.</span><span class="n">last_name</span><span class="p">))</span><span class="o">.</span><span class="n">upper</span><span class="p">()</span>
<span class="n">upper_case_name</span><span class="o">.</span><span class="n">short_description</span> <span class="o">=</span> <span class="s1">&#39;Name&#39;</span>

<span class="k">class</span> <span class="nc">PersonAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">list_display</span> <span class="o">=</span> <span class="p">(</span><span class="n">upper_case_name</span><span class="p">,)</span>
</pre></div>
</div>
</li>
<li><p class="first">A string representing an attribute on the <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code>. This
behaves same as the callable. For example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">PersonAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">list_display</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;upper_case_name&#39;</span><span class="p">,)</span>

    <span class="k">def</span> <span class="nf">upper_case_name</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">(</span><span class="s2">&quot;</span><span class="si">%s</span><span class="s2"> </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">obj</span><span class="o">.</span><span class="n">first_name</span><span class="p">,</span> <span class="n">obj</span><span class="o">.</span><span class="n">last_name</span><span class="p">))</span><span class="o">.</span><span class="n">upper</span><span class="p">()</span>
    <span class="n">upper_case_name</span><span class="o">.</span><span class="n">short_description</span> <span class="o">=</span> <span class="s1">&#39;Name&#39;</span>
</pre></div>
</div>
</li>
<li><p class="first">A string representing an attribute on the model. This behaves almost
the same as the callable, but <code class="docutils literal notranslate"><span class="pre">self</span></code> in this context is the model
instance. Here’s a full model example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.db</span> <span class="k">import</span> <span class="n">models</span>
<span class="kn">from</span> <span class="nn">django.contrib</span> <span class="k">import</span> <span class="n">admin</span>

<span class="k">class</span> <span class="nc">Person</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">50</span><span class="p">)</span>
    <span class="n">birthday</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">DateField</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">decade_born_in</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">birthday</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s1">&#39;%Y&#39;</span><span class="p">)[:</span><span class="mi">3</span><span class="p">]</span> <span class="o">+</span> <span class="s2">&quot;0&#39;s&quot;</span>
    <span class="n">decade_born_in</span><span class="o">.</span><span class="n">short_description</span> <span class="o">=</span> <span class="s1">&#39;Birth decade&#39;</span>

<span class="k">class</span> <span class="nc">PersonAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">list_display</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">,</span> <span class="s1">&#39;decade_born_in&#39;</span><span class="p">)</span>
</pre></div>
</div>
</li>
</ul>
<p>A few special cases to note about <code class="docutils literal notranslate"><span class="pre">list_display</span></code>:</p>
<ul>
<li><p class="first">If the field is a <code class="docutils literal notranslate"><span class="pre">ForeignKey</span></code>, Django will display the
<code class="docutils literal notranslate"><span class="pre">__str__()</span></code> (<code class="docutils literal notranslate"><span class="pre">__unicode__()</span></code> on Python 2) of the related object.</p>
</li>
<li><p class="first"><code class="docutils literal notranslate"><span class="pre">ManyToManyField</span></code> fields aren’t supported, because that would
entail executing a separate SQL statement for each row in the table.
If you want to do this nonetheless, give your model a custom method,
and add that method’s name to <code class="docutils literal notranslate"><span class="pre">list_display</span></code>. (See below for more
on custom methods in <code class="docutils literal notranslate"><span class="pre">list_display</span></code>.)</p>
</li>
<li><p class="first">If the field is a <code class="docutils literal notranslate"><span class="pre">BooleanField</span></code> or <code class="docutils literal notranslate"><span class="pre">NullBooleanField</span></code>, Django
will display a pretty “on” or “off” icon instead of <code class="docutils literal notranslate"><span class="pre">True</span></code> or
<code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>
</li>
<li><p class="first">If the string given is a method of the model, <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> or a
callable, Django will HTML-escape the output by default. To escape
user input and allow your own unescaped tags, use
<a class="reference internal" href="../../utils.html#django.utils.html.format_html" title="django.utils.html.format_html"><code class="xref py py-func docutils literal notranslate"><span class="pre">format_html()</span></code></a>.</p>
<p>Here’s a full example model:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.db</span> <span class="k">import</span> <span class="n">models</span>
<span class="kn">from</span> <span class="nn">django.contrib</span> <span class="k">import</span> <span class="n">admin</span>
<span class="kn">from</span> <span class="nn">django.utils.html</span> <span class="k">import</span> <span class="n">format_html</span>

<span class="k">class</span> <span class="nc">Person</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">first_name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">50</span><span class="p">)</span>
    <span class="n">last_name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">50</span><span class="p">)</span>
    <span class="n">color_code</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">6</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">colored_name</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">format_html</span><span class="p">(</span>
            <span class="s1">&#39;&lt;span style=&quot;color: #</span><span class="si">{}</span><span class="s1">;&quot;&gt;</span><span class="si">{}</span><span class="s1"> </span><span class="si">{}</span><span class="s1">&lt;/span&gt;&#39;</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">color_code</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">first_name</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">last_name</span><span class="p">,</span>
        <span class="p">)</span>

<span class="k">class</span> <span class="nc">PersonAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">list_display</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;first_name&#39;</span><span class="p">,</span> <span class="s1">&#39;last_name&#39;</span><span class="p">,</span> <span class="s1">&#39;colored_name&#39;</span><span class="p">)</span>
</pre></div>
</div>
<div class="deprecated">
<p><span class="versionmodified">Deprecated since version 1.9: </span>In older versions, you could add an <code class="docutils literal notranslate"><span class="pre">allow_tags</span></code> attribute to the
method to prevent auto-escaping. This attribute is deprecated as it’s
safer to use <a class="reference internal" href="../../utils.html#django.utils.html.format_html" title="django.utils.html.format_html"><code class="xref py py-func docutils literal notranslate"><span class="pre">format_html()</span></code></a>,
<a class="reference internal" href="../../utils.html#django.utils.html.format_html_join" title="django.utils.html.format_html_join"><code class="xref py py-func docutils literal notranslate"><span class="pre">format_html_join()</span></code></a>, or
<a class="reference internal" href="../../utils.html#django.utils.safestring.mark_safe" title="django.utils.safestring.mark_safe"><code class="xref py py-func docutils literal notranslate"><span class="pre">mark_safe()</span></code></a> instead.</p>
</div>
</li>
<li><p class="first">As some examples have already demonstrated, when using a callable, a
model method, or a <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> method, you can customize the column’s
title by adding a <code class="docutils literal notranslate"><span class="pre">short_description</span></code> attribute to the callable.</p>
</li>
<li><p class="first">If the value of a field is <code class="docutils literal notranslate"><span class="pre">None</span></code>, an empty string, or an iterable
without elements, Django will display <code class="docutils literal notranslate"><span class="pre">-</span></code> (a dash). You can override
this with <a class="reference internal" href="#django.contrib.admin.AdminSite.empty_value_display" title="django.contrib.admin.AdminSite.empty_value_display"><code class="xref py py-attr docutils literal notranslate"><span class="pre">AdminSite.empty_value_display</span></code></a>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib</span> <span class="k">import</span> <span class="n">admin</span>

<span class="n">admin</span><span class="o">.</span><span class="n">site</span><span class="o">.</span><span class="n">empty_value_display</span> <span class="o">=</span> <span class="s1">&#39;(None)&#39;</span>
</pre></div>
</div>
<p>You can also use <a class="reference internal" href="#django.contrib.admin.ModelAdmin.empty_value_display" title="django.contrib.admin.ModelAdmin.empty_value_display"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ModelAdmin.empty_value_display</span></code></a>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">PersonAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">empty_value_display</span> <span class="o">=</span> <span class="s1">&#39;unknown&#39;</span>
</pre></div>
</div>
<p>Or on a field level:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">PersonAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">list_display</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">,</span> <span class="s1">&#39;birth_date_view&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">birth_date_view</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
         <span class="k">return</span> <span class="n">obj</span><span class="o">.</span><span class="n">birth_date</span>

    <span class="n">birth_date_view</span><span class="o">.</span><span class="n">empty_value_display</span> <span class="o">=</span> <span class="s1">&#39;unknown&#39;</span>
</pre></div>
</div>
</li>
<li><p class="first">If the string given is a method of the model, <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> or a
callable that returns True or False Django will display a pretty
“on” or “off” icon if you give the method a <code class="docutils literal notranslate"><span class="pre">boolean</span></code> attribute
whose value is <code class="docutils literal notranslate"><span class="pre">True</span></code>.</p>
<p>Here’s a full example model:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.db</span> <span class="k">import</span> <span class="n">models</span>
<span class="kn">from</span> <span class="nn">django.contrib</span> <span class="k">import</span> <span class="n">admin</span>

<span class="k">class</span> <span class="nc">Person</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">first_name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">50</span><span class="p">)</span>
    <span class="n">birthday</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">DateField</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">born_in_fifties</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">birthday</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s1">&#39;%Y&#39;</span><span class="p">)[:</span><span class="mi">3</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;195&#39;</span>
    <span class="n">born_in_fifties</span><span class="o">.</span><span class="n">boolean</span> <span class="o">=</span> <span class="kc">True</span>

<span class="k">class</span> <span class="nc">PersonAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">list_display</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">,</span> <span class="s1">&#39;born_in_fifties&#39;</span><span class="p">)</span>
</pre></div>
</div>
</li>
<li><p class="first">The <code class="docutils literal notranslate"><span class="pre">__str__()</span></code> (<code class="docutils literal notranslate"><span class="pre">__unicode__()</span></code> on Python 2) method is just
as valid in <code class="docutils literal notranslate"><span class="pre">list_display</span></code> as any other model method, so it’s
perfectly OK to do this:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">list_display</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;__str__&#39;</span><span class="p">,</span> <span class="s1">&#39;some_other_field&#39;</span><span class="p">)</span>
</pre></div>
</div>
</li>
<li><p class="first">Usually, elements of <code class="docutils literal notranslate"><span class="pre">list_display</span></code> that aren’t actual database
fields can’t be used in sorting (because Django does all the sorting
at the database level).</p>
<p>However, if an element of <code class="docutils literal notranslate"><span class="pre">list_display</span></code> represents a certain
database field, you can indicate this fact by setting the
<code class="docutils literal notranslate"><span class="pre">admin_order_field</span></code> attribute of the item.</p>
<p>For example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.db</span> <span class="k">import</span> <span class="n">models</span>
<span class="kn">from</span> <span class="nn">django.contrib</span> <span class="k">import</span> <span class="n">admin</span>
<span class="kn">from</span> <span class="nn">django.utils.html</span> <span class="k">import</span> <span class="n">format_html</span>

<span class="k">class</span> <span class="nc">Person</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">first_name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">50</span><span class="p">)</span>
    <span class="n">color_code</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">6</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">colored_first_name</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">format_html</span><span class="p">(</span>
            <span class="s1">&#39;&lt;span style=&quot;color: #</span><span class="si">{}</span><span class="s1">;&quot;&gt;</span><span class="si">{}</span><span class="s1">&lt;/span&gt;&#39;</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">color_code</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">first_name</span><span class="p">,</span>
        <span class="p">)</span>

    <span class="n">colored_first_name</span><span class="o">.</span><span class="n">admin_order_field</span> <span class="o">=</span> <span class="s1">&#39;first_name&#39;</span>

<span class="k">class</span> <span class="nc">PersonAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">list_display</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;first_name&#39;</span><span class="p">,</span> <span class="s1">&#39;colored_first_name&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>The above will tell Django to order by the <code class="docutils literal notranslate"><span class="pre">first_name</span></code> field when
trying to sort by <code class="docutils literal notranslate"><span class="pre">colored_first_name</span></code> in the admin.</p>
<p>To indicate descending order with <code class="docutils literal notranslate"><span class="pre">admin_order_field</span></code> you can use a
hyphen prefix on the field name. Using the above example, this would
look like:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">colored_first_name</span><span class="o">.</span><span class="n">admin_order_field</span> <span class="o">=</span> <span class="s1">&#39;-first_name&#39;</span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">admin_order_field</span></code> supports query lookups to sort by values on related
models. This example includes an “author first name” column in the list
display and allows sorting it by first name:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Blog</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">title</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">255</span><span class="p">)</span>
    <span class="n">author</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">Person</span><span class="p">,</span> <span class="n">on_delete</span><span class="o">=</span><span class="n">models</span><span class="o">.</span><span class="n">CASCADE</span><span class="p">)</span>

<span class="k">class</span> <span class="nc">BlogAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">list_display</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;title&#39;</span><span class="p">,</span> <span class="s1">&#39;author&#39;</span><span class="p">,</span> <span class="s1">&#39;author_first_name&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">author_first_name</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">obj</span><span class="o">.</span><span class="n">author</span><span class="o">.</span><span class="n">first_name</span>

    <span class="n">author_first_name</span><span class="o">.</span><span class="n">admin_order_field</span> <span class="o">=</span> <span class="s1">&#39;author__first_name&#39;</span>
</pre></div>
</div>
</li>
<li><p class="first">Elements of <code class="docutils literal notranslate"><span class="pre">list_display</span></code> can also be properties. Please note however,
that due to the way properties work in Python, setting
<code class="docutils literal notranslate"><span class="pre">short_description</span></code> on a property is only possible when using the
<code class="docutils literal notranslate"><span class="pre">property()</span></code> function and <strong>not</strong> with the <code class="docutils literal notranslate"><span class="pre">&#64;property</span></code> decorator.</p>
<p>For example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Person</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">first_name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">50</span><span class="p">)</span>
    <span class="n">last_name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">50</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">my_property</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">first_name</span> <span class="o">+</span> <span class="s1">&#39; &#39;</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">last_name</span>
    <span class="n">my_property</span><span class="o">.</span><span class="n">short_description</span> <span class="o">=</span> <span class="s2">&quot;Full name of the person&quot;</span>

    <span class="n">full_name</span> <span class="o">=</span> <span class="nb">property</span><span class="p">(</span><span class="n">my_property</span><span class="p">)</span>

<span class="k">class</span> <span class="nc">PersonAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">list_display</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;full_name&#39;</span><span class="p">,)</span>
</pre></div>
</div>
</li>
<li><p class="first">The field names in <code class="docutils literal notranslate"><span class="pre">list_display</span></code> will also appear as CSS classes in
the HTML output, in the form of <code class="docutils literal notranslate"><span class="pre">column-&lt;field_name&gt;</span></code> on each <code class="docutils literal notranslate"><span class="pre">&lt;th&gt;</span></code>
element. This can be used to set column widths in a CSS file for example.</p>
</li>
<li><p class="first">Django will try to interpret every element of <code class="docutils literal notranslate"><span class="pre">list_display</span></code> in this
order:</p>
<ul class="simple">
<li>A field of the model.</li>
<li>A callable.</li>
<li>A string representing a <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> attribute.</li>
<li>A string representing a model attribute.</li>
</ul>
<p>For example if you have <code class="docutils literal notranslate"><span class="pre">first_name</span></code> as a model field and
as a <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> attribute, the model field will be used.</p>
</li>
</ul>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.list_display_links">
<code class="descclassname">ModelAdmin.</code><code class="descname">list_display_links</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.list_display_links" title="Permalink to this definition">¶</a></dt>
<dd><p>Use <code class="docutils literal notranslate"><span class="pre">list_display_links</span></code> to control if and which fields in
<a class="reference internal" href="#django.contrib.admin.ModelAdmin.list_display" title="django.contrib.admin.ModelAdmin.list_display"><code class="xref py py-attr docutils literal notranslate"><span class="pre">list_display</span></code></a> should be linked to the “change” page for an object.</p>
<p>By default, the change list page will link the first column – the first
field specified in <code class="docutils literal notranslate"><span class="pre">list_display</span></code> – to the change page for each item.
But <code class="docutils literal notranslate"><span class="pre">list_display_links</span></code> lets you change this:</p>
<ul>
<li><p class="first">Set it to <code class="docutils literal notranslate"><span class="pre">None</span></code> to get no links at all.</p>
</li>
<li><p class="first">Set it to a list or tuple of fields (in the same format as
<code class="docutils literal notranslate"><span class="pre">list_display</span></code>) whose columns you want converted to links.</p>
<p>You can specify one or many fields. As long as the fields appear in
<code class="docutils literal notranslate"><span class="pre">list_display</span></code>, Django doesn’t care how many (or how few) fields are
linked. The only requirement is that if you want to use
<code class="docutils literal notranslate"><span class="pre">list_display_links</span></code> in this fashion, you must define <code class="docutils literal notranslate"><span class="pre">list_display</span></code>.</p>
</li>
</ul>
<p>In this example, the <code class="docutils literal notranslate"><span class="pre">first_name</span></code> and <code class="docutils literal notranslate"><span class="pre">last_name</span></code> fields will be
linked on the change list page:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">PersonAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">list_display</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;first_name&#39;</span><span class="p">,</span> <span class="s1">&#39;last_name&#39;</span><span class="p">,</span> <span class="s1">&#39;birthday&#39;</span><span class="p">)</span>
    <span class="n">list_display_links</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;first_name&#39;</span><span class="p">,</span> <span class="s1">&#39;last_name&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>In this example, the change list page grid will have no links:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">AuditEntryAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">list_display</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;timestamp&#39;</span><span class="p">,</span> <span class="s1">&#39;message&#39;</span><span class="p">)</span>
    <span class="n">list_display_links</span> <span class="o">=</span> <span class="kc">None</span>
</pre></div>
</div>
</dd></dl>

<span class="target" id="admin-list-editable"></span><dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.list_editable">
<code class="descclassname">ModelAdmin.</code><code class="descname">list_editable</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.list_editable" title="Permalink to this definition">¶</a></dt>
<dd><p>Set <code class="docutils literal notranslate"><span class="pre">list_editable</span></code> to a list of field names on the model which will
allow editing on the change list page. That is, fields listed in
<code class="docutils literal notranslate"><span class="pre">list_editable</span></code> will be displayed as form widgets on the change list
page, allowing users to edit and save multiple rows at once.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p><code class="docutils literal notranslate"><span class="pre">list_editable</span></code> interacts with a couple of other options in
particular ways; you should note the following rules:</p>
<ul class="simple">
<li>Any field in <code class="docutils literal notranslate"><span class="pre">list_editable</span></code> must also be in <code class="docutils literal notranslate"><span class="pre">list_display</span></code>.
You can’t edit a field that’s not displayed!</li>
<li>The same field can’t be listed in both <code class="docutils literal notranslate"><span class="pre">list_editable</span></code> and
<code class="docutils literal notranslate"><span class="pre">list_display_links</span></code> – a field can’t be both a form and
a link.</li>
</ul>
<p class="last">You’ll get a validation error if either of these rules are broken.</p>
</div>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.list_filter">
<code class="descclassname">ModelAdmin.</code><code class="descname">list_filter</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.list_filter" title="Permalink to this definition">¶</a></dt>
<dd><p>Set <code class="docutils literal notranslate"><span class="pre">list_filter</span></code> to activate filters in the right sidebar of the change
list page of the admin, as illustrated in the following screenshot:</p>
<img alt="../../../_images/list_filter.png" src="../../../_images/list_filter.png" />
<p><code class="docutils literal notranslate"><span class="pre">list_filter</span></code> should be a list or tuple of elements, where each element
should be of one of the following types:</p>
<ul>
<li><p class="first">a field name, where the specified field should be either a
<code class="docutils literal notranslate"><span class="pre">BooleanField</span></code>, <code class="docutils literal notranslate"><span class="pre">CharField</span></code>, <code class="docutils literal notranslate"><span class="pre">DateField</span></code>, <code class="docutils literal notranslate"><span class="pre">DateTimeField</span></code>,
<code class="docutils literal notranslate"><span class="pre">IntegerField</span></code>, <code class="docutils literal notranslate"><span class="pre">ForeignKey</span></code> or <code class="docutils literal notranslate"><span class="pre">ManyToManyField</span></code>, for example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">PersonAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">list_filter</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;is_staff&#39;</span><span class="p">,</span> <span class="s1">&#39;company&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>Field names in <code class="docutils literal notranslate"><span class="pre">list_filter</span></code> can also span relations
using the <code class="docutils literal notranslate"><span class="pre">__</span></code> lookup, for example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">PersonAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">UserAdmin</span><span class="p">):</span>
    <span class="n">list_filter</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;company__name&#39;</span><span class="p">,)</span>
</pre></div>
</div>
</li>
<li><p class="first">a class inheriting from <code class="docutils literal notranslate"><span class="pre">django.contrib.admin.SimpleListFilter</span></code>,
which you need to provide the <code class="docutils literal notranslate"><span class="pre">title</span></code> and <code class="docutils literal notranslate"><span class="pre">parameter_name</span></code>
attributes to and override the <code class="docutils literal notranslate"><span class="pre">lookups</span></code> and <code class="docutils literal notranslate"><span class="pre">queryset</span></code> methods,
e.g.:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">datetime</span> <span class="k">import</span> <span class="n">date</span>

<span class="kn">from</span> <span class="nn">django.contrib</span> <span class="k">import</span> <span class="n">admin</span>
<span class="kn">from</span> <span class="nn">django.utils.translation</span> <span class="k">import</span> <span class="n">ugettext_lazy</span> <span class="k">as</span> <span class="n">_</span>

<span class="k">class</span> <span class="nc">DecadeBornListFilter</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">SimpleListFilter</span><span class="p">):</span>
    <span class="c1"># Human-readable title which will be displayed in the</span>
    <span class="c1"># right admin sidebar just above the filter options.</span>
    <span class="n">title</span> <span class="o">=</span> <span class="n">_</span><span class="p">(</span><span class="s1">&#39;decade born&#39;</span><span class="p">)</span>

    <span class="c1"># Parameter for the filter that will be used in the URL query.</span>
    <span class="n">parameter_name</span> <span class="o">=</span> <span class="s1">&#39;decade&#39;</span>

    <span class="k">def</span> <span class="nf">lookups</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">model_admin</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns a list of tuples. The first element in each</span>
<span class="sd">        tuple is the coded value for the option that will</span>
<span class="sd">        appear in the URL query. The second element is the</span>
<span class="sd">        human-readable name for the option that will appear</span>
<span class="sd">        in the right sidebar.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">(</span>
            <span class="p">(</span><span class="s1">&#39;80s&#39;</span><span class="p">,</span> <span class="n">_</span><span class="p">(</span><span class="s1">&#39;in the eighties&#39;</span><span class="p">)),</span>
            <span class="p">(</span><span class="s1">&#39;90s&#39;</span><span class="p">,</span> <span class="n">_</span><span class="p">(</span><span class="s1">&#39;in the nineties&#39;</span><span class="p">)),</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">queryset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">queryset</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the filtered queryset based on the value</span>
<span class="sd">        provided in the query string and retrievable via</span>
<span class="sd">        `self.value()`.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># Compare the requested value (either &#39;80s&#39; or &#39;90s&#39;)</span>
        <span class="c1"># to decide how to filter the queryset.</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">value</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;80s&#39;</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">queryset</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">birthday__gte</span><span class="o">=</span><span class="n">date</span><span class="p">(</span><span class="mi">1980</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span>
                                    <span class="n">birthday__lte</span><span class="o">=</span><span class="n">date</span><span class="p">(</span><span class="mi">1989</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">31</span><span class="p">))</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">value</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;90s&#39;</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">queryset</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">birthday__gte</span><span class="o">=</span><span class="n">date</span><span class="p">(</span><span class="mi">1990</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span>
                                    <span class="n">birthday__lte</span><span class="o">=</span><span class="n">date</span><span class="p">(</span><span class="mi">1999</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">31</span><span class="p">))</span>

<span class="k">class</span> <span class="nc">PersonAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">list_filter</span> <span class="o">=</span> <span class="p">(</span><span class="n">DecadeBornListFilter</span><span class="p">,)</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p>As a convenience, the <code class="docutils literal notranslate"><span class="pre">HttpRequest</span></code> object is passed to the
<code class="docutils literal notranslate"><span class="pre">lookups</span></code> and <code class="docutils literal notranslate"><span class="pre">queryset</span></code> methods, for example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">AuthDecadeBornListFilter</span><span class="p">(</span><span class="n">DecadeBornListFilter</span><span class="p">):</span>

    <span class="k">def</span> <span class="nf">lookups</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">model_admin</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">request</span><span class="o">.</span><span class="n">user</span><span class="o">.</span><span class="n">is_superuser</span><span class="p">:</span>
            <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">AuthDecadeBornListFilter</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">lookups</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">model_admin</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">queryset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">queryset</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">request</span><span class="o">.</span><span class="n">user</span><span class="o">.</span><span class="n">is_superuser</span><span class="p">:</span>
            <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">AuthDecadeBornListFilter</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">queryset</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">queryset</span><span class="p">)</span>
</pre></div>
</div>
<p>Also as a convenience, the <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> object is passed to
the <code class="docutils literal notranslate"><span class="pre">lookups</span></code> method, for example if you want to base the
lookups on the available data:</p>
<div class="last highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">AdvancedDecadeBornListFilter</span><span class="p">(</span><span class="n">DecadeBornListFilter</span><span class="p">):</span>

    <span class="k">def</span> <span class="nf">lookups</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">model_admin</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Only show the lookups if there actually is</span>
<span class="sd">        anyone born in the corresponding decades.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">qs</span> <span class="o">=</span> <span class="n">model_admin</span><span class="o">.</span><span class="n">get_queryset</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">qs</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">birthday__gte</span><span class="o">=</span><span class="n">date</span><span class="p">(</span><span class="mi">1980</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span>
                      <span class="n">birthday__lte</span><span class="o">=</span><span class="n">date</span><span class="p">(</span><span class="mi">1989</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">31</span><span class="p">))</span><span class="o">.</span><span class="n">exists</span><span class="p">():</span>
            <span class="k">yield</span> <span class="p">(</span><span class="s1">&#39;80s&#39;</span><span class="p">,</span> <span class="n">_</span><span class="p">(</span><span class="s1">&#39;in the eighties&#39;</span><span class="p">))</span>
        <span class="k">if</span> <span class="n">qs</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">birthday__gte</span><span class="o">=</span><span class="n">date</span><span class="p">(</span><span class="mi">1990</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span>
                      <span class="n">birthday__lte</span><span class="o">=</span><span class="n">date</span><span class="p">(</span><span class="mi">1999</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">31</span><span class="p">))</span><span class="o">.</span><span class="n">exists</span><span class="p">():</span>
            <span class="k">yield</span> <span class="p">(</span><span class="s1">&#39;90s&#39;</span><span class="p">,</span> <span class="n">_</span><span class="p">(</span><span class="s1">&#39;in the nineties&#39;</span><span class="p">))</span>
</pre></div>
</div>
</div>
</li>
<li><p class="first">a tuple, where the first element is a field name and the second
element is a class inheriting from
<code class="docutils literal notranslate"><span class="pre">django.contrib.admin.FieldListFilter</span></code>, for example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">PersonAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">list_filter</span> <span class="o">=</span> <span class="p">(</span>
        <span class="p">(</span><span class="s1">&#39;is_staff&#39;</span><span class="p">,</span> <span class="n">admin</span><span class="o">.</span><span class="n">BooleanFieldListFilter</span><span class="p">),</span>
    <span class="p">)</span>
</pre></div>
</div>
<p>You can limit the choices of a related model to the objects involved in
that relation using <code class="docutils literal notranslate"><span class="pre">RelatedOnlyFieldListFilter</span></code>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">BookAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">list_filter</span> <span class="o">=</span> <span class="p">(</span>
        <span class="p">(</span><span class="s1">&#39;author&#39;</span><span class="p">,</span> <span class="n">admin</span><span class="o">.</span><span class="n">RelatedOnlyFieldListFilter</span><span class="p">),</span>
    <span class="p">)</span>
</pre></div>
</div>
<p>Assuming <code class="docutils literal notranslate"><span class="pre">author</span></code> is a <code class="docutils literal notranslate"><span class="pre">ForeignKey</span></code> to a <code class="docutils literal notranslate"><span class="pre">User</span></code> model, this will
limit the <code class="docutils literal notranslate"><span class="pre">list_filter</span></code> choices to the users who have written a book
instead of listing all users.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The <code class="docutils literal notranslate"><span class="pre">FieldListFilter</span></code> API is considered internal and might be
changed.</p>
</div>
</li>
</ul>
<p>List filter’s typically appear only if the filter has more than one choice.
A filter’s <code class="docutils literal notranslate"><span class="pre">has_output()</span></code> method controls whether or not it appears.</p>
<p>It is possible to specify a custom template for rendering a list filter:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">FilterWithCustomTemplate</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">SimpleListFilter</span><span class="p">):</span>
    <span class="n">template</span> <span class="o">=</span> <span class="s2">&quot;custom_template.html&quot;</span>
</pre></div>
</div>
<p>See the default template provided by Django (<code class="docutils literal notranslate"><span class="pre">admin/filter.html</span></code>) for
a concrete example.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.list_max_show_all">
<code class="descclassname">ModelAdmin.</code><code class="descname">list_max_show_all</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.list_max_show_all" title="Permalink to this definition">¶</a></dt>
<dd><p>Set <code class="docutils literal notranslate"><span class="pre">list_max_show_all</span></code> to control how many items can appear on a “Show
all” admin change list page. The admin will display a “Show all” link on the
change list only if the total result count is less than or equal to this
setting. By default, this is set to <code class="docutils literal notranslate"><span class="pre">200</span></code>.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.list_per_page">
<code class="descclassname">ModelAdmin.</code><code class="descname">list_per_page</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.list_per_page" title="Permalink to this definition">¶</a></dt>
<dd><p>Set <code class="docutils literal notranslate"><span class="pre">list_per_page</span></code> to control how many items appear on each paginated
admin change list page. By default, this is set to <code class="docutils literal notranslate"><span class="pre">100</span></code>.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.list_select_related">
<code class="descclassname">ModelAdmin.</code><code class="descname">list_select_related</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.list_select_related" title="Permalink to this definition">¶</a></dt>
<dd><p>Set <code class="docutils literal notranslate"><span class="pre">list_select_related</span></code> to tell Django to use
<a class="reference internal" href="../../models/querysets.html#django.db.models.query.QuerySet.select_related" title="django.db.models.query.QuerySet.select_related"><code class="xref py py-meth docutils literal notranslate"><span class="pre">select_related()</span></code></a> in retrieving
the list of objects on the admin change list page. This can save you a
bunch of database queries.</p>
<p>The value should be either a boolean, a list or a tuple. Default is
<code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>
<p>When value is <code class="docutils literal notranslate"><span class="pre">True</span></code>, <code class="docutils literal notranslate"><span class="pre">select_related()</span></code> will always be called. When
value is set to <code class="docutils literal notranslate"><span class="pre">False</span></code>, Django will look at <code class="docutils literal notranslate"><span class="pre">list_display</span></code> and call
<code class="docutils literal notranslate"><span class="pre">select_related()</span></code> if any <code class="docutils literal notranslate"><span class="pre">ForeignKey</span></code> is present.</p>
<p>If you need more fine-grained control, use a tuple (or list) as value for
<code class="docutils literal notranslate"><span class="pre">list_select_related</span></code>. Empty tuple will prevent Django from calling
<code class="docutils literal notranslate"><span class="pre">select_related</span></code> at all. Any other tuple will be passed directly to
<code class="docutils literal notranslate"><span class="pre">select_related</span></code> as parameters. For example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">ArticleAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">list_select_related</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;author&#39;</span><span class="p">,</span> <span class="s1">&#39;category&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>will call <code class="docutils literal notranslate"><span class="pre">select_related('author',</span> <span class="pre">'category')</span></code>.</p>
<p>If you need to specify a dynamic value based on the request, you can
implement a <a class="reference internal" href="#django.contrib.admin.ModelAdmin.get_list_select_related" title="django.contrib.admin.ModelAdmin.get_list_select_related"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_list_select_related()</span></code></a> method.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.ordering">
<code class="descclassname">ModelAdmin.</code><code class="descname">ordering</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.ordering" title="Permalink to this definition">¶</a></dt>
<dd><p>Set <code class="docutils literal notranslate"><span class="pre">ordering</span></code> to specify how lists of objects should be ordered in the
Django admin views. This should be a list or tuple in the same format as a
model’s <a class="reference internal" href="../../models/options.html#django.db.models.Options.ordering" title="django.db.models.Options.ordering"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ordering</span></code></a> parameter.</p>
<p>If this isn’t provided, the Django admin will use the model’s default
ordering.</p>
<p>If you need to specify a dynamic order (for example depending on user or
language) you can implement a <a class="reference internal" href="#django.contrib.admin.ModelAdmin.get_ordering" title="django.contrib.admin.ModelAdmin.get_ordering"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_ordering()</span></code></a> method.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.paginator">
<code class="descclassname">ModelAdmin.</code><code class="descname">paginator</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.paginator" title="Permalink to this definition">¶</a></dt>
<dd><p>The paginator class to be used for pagination. By default,
<a class="reference internal" href="../../../topics/pagination.html#django.core.paginator.Paginator" title="django.core.paginator.Paginator"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.core.paginator.Paginator</span></code></a> is used. If the custom paginator
class doesn’t have the same constructor interface as
<a class="reference internal" href="../../../topics/pagination.html#django.core.paginator.Paginator" title="django.core.paginator.Paginator"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.core.paginator.Paginator</span></code></a>, you will also need to
provide an implementation for <a class="reference internal" href="#django.contrib.admin.ModelAdmin.get_paginator" title="django.contrib.admin.ModelAdmin.get_paginator"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ModelAdmin.get_paginator()</span></code></a>.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.prepopulated_fields">
<code class="descclassname">ModelAdmin.</code><code class="descname">prepopulated_fields</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.prepopulated_fields" title="Permalink to this definition">¶</a></dt>
<dd><p>Set <code class="docutils literal notranslate"><span class="pre">prepopulated_fields</span></code> to a dictionary mapping field names to the
fields it should prepopulate from:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">ArticleAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">prepopulated_fields</span> <span class="o">=</span> <span class="p">{</span><span class="s2">&quot;slug&quot;</span><span class="p">:</span> <span class="p">(</span><span class="s2">&quot;title&quot;</span><span class="p">,)}</span>
</pre></div>
</div>
<p>When set, the given fields will use a bit of JavaScript to populate from
the fields assigned. The main use for this functionality is to
automatically generate the value for <code class="docutils literal notranslate"><span class="pre">SlugField</span></code> fields from one or more
other fields. The generated value is produced by concatenating the values
of the source fields, and then by transforming that result into a valid
slug (e.g. substituting dashes for spaces).</p>
<p><code class="docutils literal notranslate"><span class="pre">prepopulated_fields</span></code> doesn’t accept <code class="docutils literal notranslate"><span class="pre">DateTimeField</span></code>, <code class="docutils literal notranslate"><span class="pre">ForeignKey</span></code>,
<code class="docutils literal notranslate"><span class="pre">OneToOneField</span></code>, and <code class="docutils literal notranslate"><span class="pre">ManyToManyField</span></code> fields.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.preserve_filters">
<code class="descclassname">ModelAdmin.</code><code class="descname">preserve_filters</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.preserve_filters" title="Permalink to this definition">¶</a></dt>
<dd><p>The admin now preserves filters on the list view after creating, editing
or deleting an object. You can restore the previous behavior of clearing
filters by setting this attribute to <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.radio_fields">
<code class="descclassname">ModelAdmin.</code><code class="descname">radio_fields</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.radio_fields" title="Permalink to this definition">¶</a></dt>
<dd><p>By default, Django’s admin uses a select-box interface (&lt;select&gt;) for
fields that are <code class="docutils literal notranslate"><span class="pre">ForeignKey</span></code> or have <code class="docutils literal notranslate"><span class="pre">choices</span></code> set. If a field is
present in <code class="docutils literal notranslate"><span class="pre">radio_fields</span></code>, Django will use a radio-button interface
instead. Assuming <code class="docutils literal notranslate"><span class="pre">group</span></code> is a <code class="docutils literal notranslate"><span class="pre">ForeignKey</span></code> on the <code class="docutils literal notranslate"><span class="pre">Person</span></code> model:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">PersonAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">radio_fields</span> <span class="o">=</span> <span class="p">{</span><span class="s2">&quot;group&quot;</span><span class="p">:</span> <span class="n">admin</span><span class="o">.</span><span class="n">VERTICAL</span><span class="p">}</span>
</pre></div>
</div>
<p>You have the choice of using <code class="docutils literal notranslate"><span class="pre">HORIZONTAL</span></code> or <code class="docutils literal notranslate"><span class="pre">VERTICAL</span></code> from the
<code class="docutils literal notranslate"><span class="pre">django.contrib.admin</span></code> module.</p>
<p>Don’t include a field in <code class="docutils literal notranslate"><span class="pre">radio_fields</span></code> unless it’s a <code class="docutils literal notranslate"><span class="pre">ForeignKey</span></code> or has
<code class="docutils literal notranslate"><span class="pre">choices</span></code> set.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.raw_id_fields">
<code class="descclassname">ModelAdmin.</code><code class="descname">raw_id_fields</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.raw_id_fields" title="Permalink to this definition">¶</a></dt>
<dd><p>By default, Django’s admin uses a select-box interface (&lt;select&gt;) for
fields that are <code class="docutils literal notranslate"><span class="pre">ForeignKey</span></code>. Sometimes you don’t want to incur the
overhead of having to select all the related instances to display in the
drop-down.</p>
<p><code class="docutils literal notranslate"><span class="pre">raw_id_fields</span></code> is a list of fields you would like to change
into an <code class="docutils literal notranslate"><span class="pre">Input</span></code> widget for either a <code class="docutils literal notranslate"><span class="pre">ForeignKey</span></code> or
<code class="docutils literal notranslate"><span class="pre">ManyToManyField</span></code>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">ArticleAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">raw_id_fields</span> <span class="o">=</span> <span class="p">(</span><span class="s2">&quot;newspaper&quot;</span><span class="p">,)</span>
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">raw_id_fields</span></code> <code class="docutils literal notranslate"><span class="pre">Input</span></code> widget should contain a primary key if the
field is a <code class="docutils literal notranslate"><span class="pre">ForeignKey</span></code> or a comma separated list of values if the field
is a <code class="docutils literal notranslate"><span class="pre">ManyToManyField</span></code>.  The <code class="docutils literal notranslate"><span class="pre">raw_id_fields</span></code> widget shows a magnifying
glass button next to the field which allows users to search for and select
a value:</p>
<img alt="../../../_images/raw_id_fields.png" src="../../../_images/raw_id_fields.png" />
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.readonly_fields">
<code class="descclassname">ModelAdmin.</code><code class="descname">readonly_fields</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.readonly_fields" title="Permalink to this definition">¶</a></dt>
<dd><p>By default the admin shows all fields as editable. Any fields in this
option (which should be a <code class="docutils literal notranslate"><span class="pre">list</span></code> or <code class="docutils literal notranslate"><span class="pre">tuple</span></code>) will display its data
as-is and non-editable; they are also excluded from the
<a class="reference internal" href="../../../topics/forms/modelforms.html#django.forms.ModelForm" title="django.forms.ModelForm"><code class="xref py py-class docutils literal notranslate"><span class="pre">ModelForm</span></code></a> used for creating and editing. Note that
when specifying <a class="reference internal" href="#django.contrib.admin.ModelAdmin.fields" title="django.contrib.admin.ModelAdmin.fields"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ModelAdmin.fields</span></code></a> or <a class="reference internal" href="#django.contrib.admin.ModelAdmin.fieldsets" title="django.contrib.admin.ModelAdmin.fieldsets"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ModelAdmin.fieldsets</span></code></a>
the read-only fields must be present to be shown (they are ignored
otherwise).</p>
<p>If <code class="docutils literal notranslate"><span class="pre">readonly_fields</span></code> is used without defining explicit ordering through
<a class="reference internal" href="#django.contrib.admin.ModelAdmin.fields" title="django.contrib.admin.ModelAdmin.fields"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ModelAdmin.fields</span></code></a> or <a class="reference internal" href="#django.contrib.admin.ModelAdmin.fieldsets" title="django.contrib.admin.ModelAdmin.fieldsets"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ModelAdmin.fieldsets</span></code></a> they will be
added last after all editable fields.</p>
<p>A read-only field can not only display data from a model’s field, it can
also display the output of a model’s method or a method of the
<code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> class itself. This is very similar to the way
<a class="reference internal" href="#django.contrib.admin.ModelAdmin.list_display" title="django.contrib.admin.ModelAdmin.list_display"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ModelAdmin.list_display</span></code></a> behaves. This provides an easy way to use
the admin interface to provide feedback on the status of the objects being
edited, for example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib</span> <span class="k">import</span> <span class="n">admin</span>
<span class="kn">from</span> <span class="nn">django.utils.html</span> <span class="k">import</span> <span class="n">format_html_join</span>
<span class="kn">from</span> <span class="nn">django.utils.safestring</span> <span class="k">import</span> <span class="n">mark_safe</span>

<span class="k">class</span> <span class="nc">PersonAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">readonly_fields</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;address_report&#39;</span><span class="p">,)</span>

    <span class="k">def</span> <span class="nf">address_report</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">instance</span><span class="p">):</span>
        <span class="c1"># assuming get_full_address() returns a list of strings</span>
        <span class="c1"># for each line of the address and you want to separate each</span>
        <span class="c1"># line by a linebreak</span>
        <span class="k">return</span> <span class="n">format_html_join</span><span class="p">(</span>
            <span class="n">mark_safe</span><span class="p">(</span><span class="s1">&#39;&lt;br/&gt;&#39;</span><span class="p">),</span>
            <span class="s1">&#39;</span><span class="si">{}</span><span class="s1">&#39;</span><span class="p">,</span>
            <span class="p">((</span><span class="n">line</span><span class="p">,)</span> <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">instance</span><span class="o">.</span><span class="n">get_full_address</span><span class="p">()),</span>
        <span class="p">)</span> <span class="ow">or</span> <span class="n">mark_safe</span><span class="p">(</span><span class="s2">&quot;&lt;span class=&#39;errors&#39;&gt;I can&#39;t determine this address.&lt;/span&gt;&quot;</span><span class="p">)</span>

    <span class="c1"># short_description functions like a model field&#39;s verbose_name</span>
    <span class="n">address_report</span><span class="o">.</span><span class="n">short_description</span> <span class="o">=</span> <span class="s2">&quot;Address&quot;</span>
</pre></div>
</div>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.save_as">
<code class="descclassname">ModelAdmin.</code><code class="descname">save_as</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.save_as" title="Permalink to this definition">¶</a></dt>
<dd><p>Set <code class="docutils literal notranslate"><span class="pre">save_as</span></code> to enable a “save as new” feature on admin change forms.</p>
<p>Normally, objects have three save options: “Save”, “Save and continue
editing”, and “Save and add another”. If <code class="docutils literal notranslate"><span class="pre">save_as</span></code> is <code class="docutils literal notranslate"><span class="pre">True</span></code>, “Save
and add another” will be replaced by a “Save as new” button that creates a
new object (with a new ID) rather than updating the existing object.</p>
<p>By default, <code class="docutils literal notranslate"><span class="pre">save_as</span></code> is set to <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.save_as_continue">
<code class="descclassname">ModelAdmin.</code><code class="descname">save_as_continue</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.save_as_continue" title="Permalink to this definition">¶</a></dt>
<dd><div class="versionadded">
<span class="title">New in Django 1.10.</span> </div>
<p>When <a class="reference internal" href="#django.contrib.admin.ModelAdmin.save_as" title="django.contrib.admin.ModelAdmin.save_as"><code class="xref py py-attr docutils literal notranslate"><span class="pre">save_as=True</span></code></a>, the default redirect after saving the
new object is to the change view for that object. If you set
<code class="docutils literal notranslate"><span class="pre">save_as_continue=False</span></code>, the redirect will be to the changelist view.</p>
<p>By default, <code class="docutils literal notranslate"><span class="pre">save_as_continue</span></code> is set to <code class="docutils literal notranslate"><span class="pre">True</span></code>.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.save_on_top">
<code class="descclassname">ModelAdmin.</code><code class="descname">save_on_top</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.save_on_top" title="Permalink to this definition">¶</a></dt>
<dd><p>Set <code class="docutils literal notranslate"><span class="pre">save_on_top</span></code> to add save buttons across the top of your admin change
forms.</p>
<p>Normally, the save buttons appear only at the bottom of the forms. If you
set <code class="docutils literal notranslate"><span class="pre">save_on_top</span></code>, the buttons will appear both on the top and the
bottom.</p>
<p>By default, <code class="docutils literal notranslate"><span class="pre">save_on_top</span></code> is set to <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.search_fields">
<code class="descclassname">ModelAdmin.</code><code class="descname">search_fields</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.search_fields" title="Permalink to this definition">¶</a></dt>
<dd><p>Set <code class="docutils literal notranslate"><span class="pre">search_fields</span></code> to enable a search box on the admin change list page.
This should be set to a list of field names that will be searched whenever
somebody submits a search query in that text box.</p>
<p>These fields should be some kind of text field, such as <code class="docutils literal notranslate"><span class="pre">CharField</span></code> or
<code class="docutils literal notranslate"><span class="pre">TextField</span></code>. You can also perform a related lookup on a <code class="docutils literal notranslate"><span class="pre">ForeignKey</span></code> or
<code class="docutils literal notranslate"><span class="pre">ManyToManyField</span></code> with the lookup API “follow” notation:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">search_fields</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;foreign_key__related_fieldname&#39;</span><span class="p">]</span>
</pre></div>
</div>
<p>For example, if you have a blog entry with an author, the following
definition would enable searching blog entries by the email address of the
author:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">search_fields</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;user__email&#39;</span><span class="p">]</span>
</pre></div>
</div>
<p>When somebody does a search in the admin search box, Django splits the
search query into words and returns all objects that contain each of the
words, case insensitive, where each word must be in at least one of
<code class="docutils literal notranslate"><span class="pre">search_fields</span></code>. For example, if <code class="docutils literal notranslate"><span class="pre">search_fields</span></code> is set to
<code class="docutils literal notranslate"><span class="pre">['first_name',</span> <span class="pre">'last_name']</span></code> and a user searches for <code class="docutils literal notranslate"><span class="pre">john</span> <span class="pre">lennon</span></code>,
Django will do the equivalent of this SQL <code class="docutils literal notranslate"><span class="pre">WHERE</span></code> clause:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">WHERE</span> <span class="p">(</span><span class="n">first_name</span> <span class="n">ILIKE</span> <span class="s1">&#39;%john%&#39;</span> <span class="n">OR</span> <span class="n">last_name</span> <span class="n">ILIKE</span> <span class="s1">&#39;%john%&#39;</span><span class="p">)</span>
<span class="n">AND</span> <span class="p">(</span><span class="n">first_name</span> <span class="n">ILIKE</span> <span class="s1">&#39;</span><span class="si">%le</span><span class="s1">nnon%&#39;</span> <span class="n">OR</span> <span class="n">last_name</span> <span class="n">ILIKE</span> <span class="s1">&#39;</span><span class="si">%le</span><span class="s1">nnon%&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>For faster and/or more restrictive searches, prefix the field name
with an operator:</p>
<dl class="docutils">
<dt><code class="docutils literal notranslate"><span class="pre">^</span></code></dt>
<dd><p class="first">Use the ‘^’ operator to match starting at the beginning of the
field. For example, if <code class="docutils literal notranslate"><span class="pre">search_fields</span></code> is set to
<code class="docutils literal notranslate"><span class="pre">['^first_name',</span> <span class="pre">'^last_name']</span></code> and a user searches for
<code class="docutils literal notranslate"><span class="pre">john</span> <span class="pre">lennon</span></code>, Django will do the equivalent of this SQL <code class="docutils literal notranslate"><span class="pre">WHERE</span></code>
clause:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">WHERE</span> <span class="p">(</span><span class="n">first_name</span> <span class="n">ILIKE</span> <span class="s1">&#39;john%&#39;</span> <span class="n">OR</span> <span class="n">last_name</span> <span class="n">ILIKE</span> <span class="s1">&#39;john%&#39;</span><span class="p">)</span>
<span class="n">AND</span> <span class="p">(</span><span class="n">first_name</span> <span class="n">ILIKE</span> <span class="s1">&#39;lennon%&#39;</span> <span class="n">OR</span> <span class="n">last_name</span> <span class="n">ILIKE</span> <span class="s1">&#39;lennon%&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p class="last">This query is more efficient than the normal <code class="docutils literal notranslate"><span class="pre">'%john%'</span></code> query,
because the database only needs to check the beginning of a column’s
data, rather than seeking through the entire column’s data. Plus, if
the column has an index on it, some databases may be able to use the
index for this query, even though it’s a <code class="docutils literal notranslate"><span class="pre">LIKE</span></code> query.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">=</span></code></dt>
<dd><p class="first">Use the ‘=’ operator for case-insensitive exact matching. For
example, if <code class="docutils literal notranslate"><span class="pre">search_fields</span></code> is set to
<code class="docutils literal notranslate"><span class="pre">['=first_name',</span> <span class="pre">'=last_name']</span></code> and a user searches for
<code class="docutils literal notranslate"><span class="pre">john</span> <span class="pre">lennon</span></code>, Django will do the equivalent of this SQL
<code class="docutils literal notranslate"><span class="pre">WHERE</span></code> clause:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">WHERE</span> <span class="p">(</span><span class="n">first_name</span> <span class="n">ILIKE</span> <span class="s1">&#39;john&#39;</span> <span class="n">OR</span> <span class="n">last_name</span> <span class="n">ILIKE</span> <span class="s1">&#39;john&#39;</span><span class="p">)</span>
<span class="n">AND</span> <span class="p">(</span><span class="n">first_name</span> <span class="n">ILIKE</span> <span class="s1">&#39;lennon&#39;</span> <span class="n">OR</span> <span class="n">last_name</span> <span class="n">ILIKE</span> <span class="s1">&#39;lennon&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p class="last">Note that the query input is split by spaces, so, following this
example, it’s currently not possible to search for all records in which
<code class="docutils literal notranslate"><span class="pre">first_name</span></code> is exactly <code class="docutils literal notranslate"><span class="pre">'john</span> <span class="pre">winston'</span></code> (containing a space).</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">&#64;</span></code></dt>
<dd>Using the ‘&#64;’ operator to perform a full text match. This is like the
default search method but uses an index. Currently this is only
available for MySQL.</dd>
</dl>
<p>If you need to customize search you can use
<a class="reference internal" href="#django.contrib.admin.ModelAdmin.get_search_results" title="django.contrib.admin.ModelAdmin.get_search_results"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ModelAdmin.get_search_results()</span></code></a> to provide additional or alternate
search behavior.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.show_full_result_count">
<code class="descclassname">ModelAdmin.</code><code class="descname">show_full_result_count</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.show_full_result_count" title="Permalink to this definition">¶</a></dt>
<dd><p>Set <code class="docutils literal notranslate"><span class="pre">show_full_result_count</span></code> to control whether the full count of objects
should be displayed on a filtered admin page (e.g. <code class="docutils literal notranslate"><span class="pre">99</span> <span class="pre">results</span> <span class="pre">(103</span> <span class="pre">total)</span></code>).
If this option is set to <code class="docutils literal notranslate"><span class="pre">False</span></code>, a text like <code class="docutils literal notranslate"><span class="pre">99</span> <span class="pre">results</span> <span class="pre">(Show</span> <span class="pre">all)</span></code>
is displayed instead.</p>
<p>The default of <code class="docutils literal notranslate"><span class="pre">show_full_result_count=True</span></code> generates a query to perform
a full count on the table which can be expensive if the table contains a
large number of rows.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.view_on_site">
<code class="descclassname">ModelAdmin.</code><code class="descname">view_on_site</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.view_on_site" title="Permalink to this definition">¶</a></dt>
<dd><p>Set <code class="docutils literal notranslate"><span class="pre">view_on_site</span></code> to control whether or not to display the “View on site” link.
This link should bring you to a URL where you can display the saved object.</p>
<p>This value can be either a boolean flag or a callable. If <code class="docutils literal notranslate"><span class="pre">True</span></code> (the
default), the object’s <a class="reference internal" href="../../models/instances.html#django.db.models.Model.get_absolute_url" title="django.db.models.Model.get_absolute_url"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_absolute_url()</span></code></a>
method will be used to generate the url.</p>
<p>If your model has a <a class="reference internal" href="../../models/instances.html#django.db.models.Model.get_absolute_url" title="django.db.models.Model.get_absolute_url"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_absolute_url()</span></code></a> method
but you don’t want the “View on site” button to appear, you only need to set
<code class="docutils literal notranslate"><span class="pre">view_on_site</span></code> to <code class="docutils literal notranslate"><span class="pre">False</span></code>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib</span> <span class="k">import</span> <span class="n">admin</span>

<span class="k">class</span> <span class="nc">PersonAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">view_on_site</span> <span class="o">=</span> <span class="kc">False</span>
</pre></div>
</div>
<p>In case it is a callable, it accepts the model instance as a parameter.
For example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib</span> <span class="k">import</span> <span class="n">admin</span>
<span class="kn">from</span> <span class="nn">django.urls</span> <span class="k">import</span> <span class="n">reverse</span>

<span class="k">class</span> <span class="nc">PersonAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">view_on_site</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
        <span class="n">url</span> <span class="o">=</span> <span class="n">reverse</span><span class="p">(</span><span class="s1">&#39;person-detail&#39;</span><span class="p">,</span> <span class="n">kwargs</span><span class="o">=</span><span class="p">{</span><span class="s1">&#39;slug&#39;</span><span class="p">:</span> <span class="n">obj</span><span class="o">.</span><span class="n">slug</span><span class="p">})</span>
        <span class="k">return</span> <span class="s1">&#39;https://example.com&#39;</span> <span class="o">+</span> <span class="n">url</span>
</pre></div>
</div>
</dd></dl>

<div class="section" id="s-custom-template-options">
<span id="custom-template-options"></span><h4>Custom template options<a class="headerlink" href="#custom-template-options" title="Permalink to this headline">¶</a></h4>
<p>The <a class="reference internal" href="#admin-overriding-templates"><span class="std std-ref">Overriding admin templates</span></a> section describes how to override or extend
the default admin templates.  Use the following options to override the default
templates used by the <a class="reference internal" href="#django.contrib.admin.ModelAdmin" title="django.contrib.admin.ModelAdmin"><code class="xref py py-class docutils literal notranslate"><span class="pre">ModelAdmin</span></code></a> views:</p>
<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.add_form_template">
<code class="descclassname">ModelAdmin.</code><code class="descname">add_form_template</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.add_form_template" title="Permalink to this definition">¶</a></dt>
<dd><p>Path to a custom template, used by <a class="reference internal" href="#django.contrib.admin.ModelAdmin.add_view" title="django.contrib.admin.ModelAdmin.add_view"><code class="xref py py-meth docutils literal notranslate"><span class="pre">add_view()</span></code></a>.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.change_form_template">
<code class="descclassname">ModelAdmin.</code><code class="descname">change_form_template</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.change_form_template" title="Permalink to this definition">¶</a></dt>
<dd><p>Path to a custom template, used by <a class="reference internal" href="#django.contrib.admin.ModelAdmin.change_view" title="django.contrib.admin.ModelAdmin.change_view"><code class="xref py py-meth docutils literal notranslate"><span class="pre">change_view()</span></code></a>.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.change_list_template">
<code class="descclassname">ModelAdmin.</code><code class="descname">change_list_template</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.change_list_template" title="Permalink to this definition">¶</a></dt>
<dd><p>Path to a custom template, used by <a class="reference internal" href="#django.contrib.admin.ModelAdmin.changelist_view" title="django.contrib.admin.ModelAdmin.changelist_view"><code class="xref py py-meth docutils literal notranslate"><span class="pre">changelist_view()</span></code></a>.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.delete_confirmation_template">
<code class="descclassname">ModelAdmin.</code><code class="descname">delete_confirmation_template</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.delete_confirmation_template" title="Permalink to this definition">¶</a></dt>
<dd><p>Path to a custom template, used by <a class="reference internal" href="#django.contrib.admin.ModelAdmin.delete_view" title="django.contrib.admin.ModelAdmin.delete_view"><code class="xref py py-meth docutils literal notranslate"><span class="pre">delete_view()</span></code></a> for displaying a
confirmation page when deleting one or more objects.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.delete_selected_confirmation_template">
<code class="descclassname">ModelAdmin.</code><code class="descname">delete_selected_confirmation_template</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.delete_selected_confirmation_template" title="Permalink to this definition">¶</a></dt>
<dd><p>Path to a custom template, used by the <code class="docutils literal notranslate"><span class="pre">delete_selected</span></code> action method
for displaying a confirmation page when deleting one or more objects. See
the <a class="reference internal" href="actions.html"><span class="doc">actions documentation</span></a>.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.object_history_template">
<code class="descclassname">ModelAdmin.</code><code class="descname">object_history_template</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.object_history_template" title="Permalink to this definition">¶</a></dt>
<dd><p>Path to a custom template, used by <a class="reference internal" href="#django.contrib.admin.ModelAdmin.history_view" title="django.contrib.admin.ModelAdmin.history_view"><code class="xref py py-meth docutils literal notranslate"><span class="pre">history_view()</span></code></a>.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.popup_response_template">
<code class="descclassname">ModelAdmin.</code><code class="descname">popup_response_template</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.popup_response_template" title="Permalink to this definition">¶</a></dt>
<dd><div class="versionadded">
<span class="title">New in Django 1.11.</span> </div>
<p>Path to a custom template, used by <a class="reference internal" href="#django.contrib.admin.ModelAdmin.response_add" title="django.contrib.admin.ModelAdmin.response_add"><code class="xref py py-meth docutils literal notranslate"><span class="pre">response_add()</span></code></a>,
<a class="reference internal" href="#django.contrib.admin.ModelAdmin.response_change" title="django.contrib.admin.ModelAdmin.response_change"><code class="xref py py-meth docutils literal notranslate"><span class="pre">response_change()</span></code></a>, and <a class="reference internal" href="#django.contrib.admin.ModelAdmin.response_delete" title="django.contrib.admin.ModelAdmin.response_delete"><code class="xref py py-meth docutils literal notranslate"><span class="pre">response_delete()</span></code></a>.</p>
</dd></dl>

</div>
</div>
<div class="section" id="s-modeladmin-methods">
<span id="s-model-admin-methods"></span><span id="modeladmin-methods"></span><span id="model-admin-methods"></span><h3><code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> methods<a class="headerlink" href="#modeladmin-methods" title="Permalink to this headline">¶</a></h3>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">When overriding <a class="reference internal" href="#django.contrib.admin.ModelAdmin.save_model" title="django.contrib.admin.ModelAdmin.save_model"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ModelAdmin.save_model()</span></code></a> and
<a class="reference internal" href="#django.contrib.admin.ModelAdmin.delete_model" title="django.contrib.admin.ModelAdmin.delete_model"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ModelAdmin.delete_model()</span></code></a>, your code must save/delete the
object. They aren’t meant for veto purposes, rather they allow you to
perform extra operations.</p>
</div>
<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.save_model">
<code class="descclassname">ModelAdmin.</code><code class="descname">save_model</code>(<em>request</em>, <em>obj</em>, <em>form</em>, <em>change</em>)<a class="reference internal" href="../../../_modules/django/contrib/admin/options.html#ModelAdmin.save_model"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.contrib.admin.ModelAdmin.save_model" title="Permalink to this definition">¶</a></dt>
<dd><p>The <code class="docutils literal notranslate"><span class="pre">save_model</span></code> method is given the <code class="docutils literal notranslate"><span class="pre">HttpRequest</span></code>, a model instance,
a <code class="docutils literal notranslate"><span class="pre">ModelForm</span></code> instance, and a boolean value based on whether it is adding
or changing the object. Overriding this method allows doing pre- or
post-save operations. Call <code class="docutils literal notranslate"><span class="pre">super().save_model()</span></code> to save the object
using <a class="reference internal" href="../../models/instances.html#django.db.models.Model.save" title="django.db.models.Model.save"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Model.save()</span></code></a>.</p>
<p>For example to attach <code class="docutils literal notranslate"><span class="pre">request.user</span></code> to the object prior to saving:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib</span> <span class="k">import</span> <span class="n">admin</span>

<span class="k">class</span> <span class="nc">ArticleAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">save_model</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">,</span> <span class="n">form</span><span class="p">,</span> <span class="n">change</span><span class="p">):</span>
        <span class="n">obj</span><span class="o">.</span><span class="n">user</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">user</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">ArticleAdmin</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">save_model</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">,</span> <span class="n">form</span><span class="p">,</span> <span class="n">change</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.delete_model">
<code class="descclassname">ModelAdmin.</code><code class="descname">delete_model</code>(<em>request</em>, <em>obj</em>)<a class="reference internal" href="../../../_modules/django/contrib/admin/options.html#ModelAdmin.delete_model"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.contrib.admin.ModelAdmin.delete_model" title="Permalink to this definition">¶</a></dt>
<dd><p>The <code class="docutils literal notranslate"><span class="pre">delete_model</span></code> method is given the <code class="docutils literal notranslate"><span class="pre">HttpRequest</span></code> and a model
instance. Overriding this method allows doing pre- or post-delete
operations. Call <code class="docutils literal notranslate"><span class="pre">super().delete_model()</span></code> to delete the object using
<a class="reference internal" href="../../models/instances.html#django.db.models.Model.delete" title="django.db.models.Model.delete"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Model.delete()</span></code></a>.</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.save_formset">
<code class="descclassname">ModelAdmin.</code><code class="descname">save_formset</code>(<em>request</em>, <em>form</em>, <em>formset</em>, <em>change</em>)<a class="reference internal" href="../../../_modules/django/contrib/admin/options.html#ModelAdmin.save_formset"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.contrib.admin.ModelAdmin.save_formset" title="Permalink to this definition">¶</a></dt>
<dd><p>The <code class="docutils literal notranslate"><span class="pre">save_formset</span></code> method is given the <code class="docutils literal notranslate"><span class="pre">HttpRequest</span></code>, the parent
<code class="docutils literal notranslate"><span class="pre">ModelForm</span></code> instance and a boolean value based on whether it is adding or
changing the parent object.</p>
<p>For example, to attach <code class="docutils literal notranslate"><span class="pre">request.user</span></code> to each changed formset
model instance:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">ArticleAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">save_formset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">form</span><span class="p">,</span> <span class="n">formset</span><span class="p">,</span> <span class="n">change</span><span class="p">):</span>
        <span class="n">instances</span> <span class="o">=</span> <span class="n">formset</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">commit</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">obj</span> <span class="ow">in</span> <span class="n">formset</span><span class="o">.</span><span class="n">deleted_objects</span><span class="p">:</span>
            <span class="n">obj</span><span class="o">.</span><span class="n">delete</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">instance</span> <span class="ow">in</span> <span class="n">instances</span><span class="p">:</span>
            <span class="n">instance</span><span class="o">.</span><span class="n">user</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">user</span>
            <span class="n">instance</span><span class="o">.</span><span class="n">save</span><span class="p">()</span>
        <span class="n">formset</span><span class="o">.</span><span class="n">save_m2m</span><span class="p">()</span>
</pre></div>
</div>
<p>See also <a class="reference internal" href="../../../topics/forms/modelforms.html#saving-objects-in-the-formset"><span class="std std-ref">Saving objects in the formset</span></a>.</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.get_ordering">
<code class="descclassname">ModelAdmin.</code><code class="descname">get_ordering</code>(<em>request</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.get_ordering" title="Permalink to this definition">¶</a></dt>
<dd><p>The <code class="docutils literal notranslate"><span class="pre">get_ordering</span></code> method takes a <code class="docutils literal notranslate"><span class="pre">request</span></code> as parameter and
is expected to return a <code class="docutils literal notranslate"><span class="pre">list</span></code> or <code class="docutils literal notranslate"><span class="pre">tuple</span></code> for ordering similar
to the <a class="reference internal" href="#django.contrib.admin.ModelAdmin.ordering" title="django.contrib.admin.ModelAdmin.ordering"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ordering</span></code></a> attribute. For example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">PersonAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>

    <span class="k">def</span> <span class="nf">get_ordering</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">request</span><span class="o">.</span><span class="n">user</span><span class="o">.</span><span class="n">is_superuser</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">[</span><span class="s1">&#39;name&#39;</span><span class="p">,</span> <span class="s1">&#39;rank&#39;</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">[</span><span class="s1">&#39;name&#39;</span><span class="p">]</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.get_search_results">
<code class="descclassname">ModelAdmin.</code><code class="descname">get_search_results</code>(<em>request</em>, <em>queryset</em>, <em>search_term</em>)<a class="reference internal" href="../../../_modules/django/contrib/admin/options.html#ModelAdmin.get_search_results"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.contrib.admin.ModelAdmin.get_search_results" title="Permalink to this definition">¶</a></dt>
<dd><p>The <code class="docutils literal notranslate"><span class="pre">get_search_results</span></code> method modifies the list of objects displayed
into those that match the provided search term. It accepts the request, a
queryset that applies the current filters, and the user-provided search term.
It returns a tuple containing a queryset modified to implement the search, and
a boolean indicating if the results may contain duplicates.</p>
<p>The default implementation searches the fields named in <a class="reference internal" href="#django.contrib.admin.ModelAdmin.search_fields" title="django.contrib.admin.ModelAdmin.search_fields"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ModelAdmin.search_fields</span></code></a>.</p>
<p>This method may be overridden with your own custom search method. For
example, you might wish to search by an integer field, or use an external
tool such as Solr or Haystack. You must establish if the queryset changes
implemented by your search method may introduce duplicates into the results,
and return <code class="docutils literal notranslate"><span class="pre">True</span></code> in the second element of the return value.</p>
<p>For example, to search by <code class="docutils literal notranslate"><span class="pre">name</span></code> and <code class="docutils literal notranslate"><span class="pre">age</span></code>, you could use:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">PersonAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">list_display</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">,</span> <span class="s1">&#39;age&#39;</span><span class="p">)</span>
    <span class="n">search_fields</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">,)</span>

    <span class="k">def</span> <span class="nf">get_search_results</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">queryset</span><span class="p">,</span> <span class="n">search_term</span><span class="p">):</span>
        <span class="n">queryset</span><span class="p">,</span> <span class="n">use_distinct</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">PersonAdmin</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">get_search_results</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">queryset</span><span class="p">,</span> <span class="n">search_term</span><span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">search_term_as_int</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">search_term</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
            <span class="k">pass</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">queryset</span> <span class="o">|=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">age</span><span class="o">=</span><span class="n">search_term_as_int</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">queryset</span><span class="p">,</span> <span class="n">use_distinct</span>
</pre></div>
</div>
<p>This implementation is more efficient than <code class="docutils literal notranslate"><span class="pre">search_fields</span> <span class="pre">=</span>
<span class="pre">('name',</span> <span class="pre">'=age')</span></code> which results in a string comparison for the numeric
field, for example <code class="docutils literal notranslate"><span class="pre">...</span> <span class="pre">OR</span> <span class="pre">UPPER(&quot;polls_choice&quot;.&quot;votes&quot;::text)</span> <span class="pre">=</span> <span class="pre">UPPER('4')</span></code>
on PostgreSQL.</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.save_related">
<code class="descclassname">ModelAdmin.</code><code class="descname">save_related</code>(<em>request</em>, <em>form</em>, <em>formsets</em>, <em>change</em>)<a class="reference internal" href="../../../_modules/django/contrib/admin/options.html#ModelAdmin.save_related"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.contrib.admin.ModelAdmin.save_related" title="Permalink to this definition">¶</a></dt>
<dd><p>The <code class="docutils literal notranslate"><span class="pre">save_related</span></code> method is given the <code class="docutils literal notranslate"><span class="pre">HttpRequest</span></code>, the parent
<code class="docutils literal notranslate"><span class="pre">ModelForm</span></code> instance, the list of inline formsets and a boolean value
based on whether the parent is being added or changed. Here you can do any
pre- or post-save operations for objects related to the parent. Note
that at this point the parent object and its form have already been saved.</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.get_readonly_fields">
<code class="descclassname">ModelAdmin.</code><code class="descname">get_readonly_fields</code>(<em>request</em>, <em>obj=None</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.get_readonly_fields" title="Permalink to this definition">¶</a></dt>
<dd><p>The <code class="docutils literal notranslate"><span class="pre">get_readonly_fields</span></code> method is given the <code class="docutils literal notranslate"><span class="pre">HttpRequest</span></code> and the
<code class="docutils literal notranslate"><span class="pre">obj</span></code> being edited (or <code class="docutils literal notranslate"><span class="pre">None</span></code> on an add form) and is expected to return
a <code class="docutils literal notranslate"><span class="pre">list</span></code> or <code class="docutils literal notranslate"><span class="pre">tuple</span></code> of field names that will be displayed as read-only,
as described above in the <a class="reference internal" href="#django.contrib.admin.ModelAdmin.readonly_fields" title="django.contrib.admin.ModelAdmin.readonly_fields"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ModelAdmin.readonly_fields</span></code></a> section.</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.get_prepopulated_fields">
<code class="descclassname">ModelAdmin.</code><code class="descname">get_prepopulated_fields</code>(<em>request</em>, <em>obj=None</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.get_prepopulated_fields" title="Permalink to this definition">¶</a></dt>
<dd><p>The <code class="docutils literal notranslate"><span class="pre">get_prepopulated_fields</span></code> method is given the <code class="docutils literal notranslate"><span class="pre">HttpRequest</span></code> and the
<code class="docutils literal notranslate"><span class="pre">obj</span></code> being edited (or <code class="docutils literal notranslate"><span class="pre">None</span></code> on an add form) and is expected to return
a <code class="docutils literal notranslate"><span class="pre">dictionary</span></code>, as described above in the <a class="reference internal" href="#django.contrib.admin.ModelAdmin.prepopulated_fields" title="django.contrib.admin.ModelAdmin.prepopulated_fields"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ModelAdmin.prepopulated_fields</span></code></a>
section.</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.get_list_display">
<code class="descclassname">ModelAdmin.</code><code class="descname">get_list_display</code>(<em>request</em>)<a class="reference internal" href="../../../_modules/django/contrib/admin/options.html#ModelAdmin.get_list_display"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.contrib.admin.ModelAdmin.get_list_display" title="Permalink to this definition">¶</a></dt>
<dd><p>The <code class="docutils literal notranslate"><span class="pre">get_list_display</span></code> method is given the <code class="docutils literal notranslate"><span class="pre">HttpRequest</span></code> and is
expected to return a <code class="docutils literal notranslate"><span class="pre">list</span></code> or <code class="docutils literal notranslate"><span class="pre">tuple</span></code> of field names that will be
displayed on the changelist view as described above in the
<a class="reference internal" href="#django.contrib.admin.ModelAdmin.list_display" title="django.contrib.admin.ModelAdmin.list_display"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ModelAdmin.list_display</span></code></a> section.</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.get_list_display_links">
<code class="descclassname">ModelAdmin.</code><code class="descname">get_list_display_links</code>(<em>request</em>, <em>list_display</em>)<a class="reference internal" href="../../../_modules/django/contrib/admin/options.html#ModelAdmin.get_list_display_links"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.contrib.admin.ModelAdmin.get_list_display_links" title="Permalink to this definition">¶</a></dt>
<dd><p>The <code class="docutils literal notranslate"><span class="pre">get_list_display_links</span></code> method is given the <code class="docutils literal notranslate"><span class="pre">HttpRequest</span></code> and
the <code class="docutils literal notranslate"><span class="pre">list</span></code> or <code class="docutils literal notranslate"><span class="pre">tuple</span></code> returned by <a class="reference internal" href="#django.contrib.admin.ModelAdmin.get_list_display" title="django.contrib.admin.ModelAdmin.get_list_display"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ModelAdmin.get_list_display()</span></code></a>.
It is expected to return either <code class="docutils literal notranslate"><span class="pre">None</span></code> or a <code class="docutils literal notranslate"><span class="pre">list</span></code> or <code class="docutils literal notranslate"><span class="pre">tuple</span></code> of field
names on the changelist that will be linked to the change view, as described
in the <a class="reference internal" href="#django.contrib.admin.ModelAdmin.list_display_links" title="django.contrib.admin.ModelAdmin.list_display_links"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ModelAdmin.list_display_links</span></code></a> section.</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.get_exclude">
<code class="descclassname">ModelAdmin.</code><code class="descname">get_exclude</code>(<em>request</em>, <em>obj=None</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.get_exclude" title="Permalink to this definition">¶</a></dt>
<dd><div class="versionadded">
<span class="title">New in Django 1.11.</span> </div>
<p>The <code class="docutils literal notranslate"><span class="pre">get_exclude</span></code> method is given the <code class="docutils literal notranslate"><span class="pre">HttpRequest</span></code> and the <code class="docutils literal notranslate"><span class="pre">obj</span></code>
being edited (or <code class="docutils literal notranslate"><span class="pre">None</span></code> on an add form) and is expected to return a list
of fields, as described in <a class="reference internal" href="#django.contrib.admin.ModelAdmin.exclude" title="django.contrib.admin.ModelAdmin.exclude"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ModelAdmin.exclude</span></code></a>.</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.get_fields">
<code class="descclassname">ModelAdmin.</code><code class="descname">get_fields</code>(<em>request</em>, <em>obj=None</em>)<a class="reference internal" href="../../../_modules/django/contrib/admin/options.html#ModelAdmin.get_fields"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.contrib.admin.ModelAdmin.get_fields" title="Permalink to this definition">¶</a></dt>
<dd><p>The <code class="docutils literal notranslate"><span class="pre">get_fields</span></code> method is given the <code class="docutils literal notranslate"><span class="pre">HttpRequest</span></code> and the <code class="docutils literal notranslate"><span class="pre">obj</span></code>
being edited (or <code class="docutils literal notranslate"><span class="pre">None</span></code> on an add form) and is expected to return a list
of fields, as described above in the <a class="reference internal" href="#django.contrib.admin.ModelAdmin.fields" title="django.contrib.admin.ModelAdmin.fields"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ModelAdmin.fields</span></code></a> section.</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.get_fieldsets">
<code class="descclassname">ModelAdmin.</code><code class="descname">get_fieldsets</code>(<em>request</em>, <em>obj=None</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.get_fieldsets" title="Permalink to this definition">¶</a></dt>
<dd><p>The <code class="docutils literal notranslate"><span class="pre">get_fieldsets</span></code> method is given the <code class="docutils literal notranslate"><span class="pre">HttpRequest</span></code> and the <code class="docutils literal notranslate"><span class="pre">obj</span></code>
being edited (or <code class="docutils literal notranslate"><span class="pre">None</span></code> on an add form) and is expected to return a list
of two-tuples, in which each two-tuple represents a <code class="docutils literal notranslate"><span class="pre">&lt;fieldset&gt;</span></code> on the
admin form page, as described above in the <a class="reference internal" href="#django.contrib.admin.ModelAdmin.fieldsets" title="django.contrib.admin.ModelAdmin.fieldsets"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ModelAdmin.fieldsets</span></code></a> section.</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.get_list_filter">
<code class="descclassname">ModelAdmin.</code><code class="descname">get_list_filter</code>(<em>request</em>)<a class="reference internal" href="../../../_modules/django/contrib/admin/options.html#ModelAdmin.get_list_filter"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.contrib.admin.ModelAdmin.get_list_filter" title="Permalink to this definition">¶</a></dt>
<dd><p>The <code class="docutils literal notranslate"><span class="pre">get_list_filter</span></code> method is given the <code class="docutils literal notranslate"><span class="pre">HttpRequest</span></code> and is expected
to return the same kind of sequence type as for the
<a class="reference internal" href="#django.contrib.admin.ModelAdmin.list_filter" title="django.contrib.admin.ModelAdmin.list_filter"><code class="xref py py-attr docutils literal notranslate"><span class="pre">list_filter</span></code></a> attribute.</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.get_list_select_related">
<code class="descclassname">ModelAdmin.</code><code class="descname">get_list_select_related</code>(<em>request</em>)<a class="reference internal" href="../../../_modules/django/contrib/admin/options.html#ModelAdmin.get_list_select_related"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.contrib.admin.ModelAdmin.get_list_select_related" title="Permalink to this definition">¶</a></dt>
<dd><p>The <code class="docutils literal notranslate"><span class="pre">get_list_select_related</span></code> method is given the <code class="docutils literal notranslate"><span class="pre">HttpRequest</span></code> and
should return a boolean or list as <a class="reference internal" href="#django.contrib.admin.ModelAdmin.list_select_related" title="django.contrib.admin.ModelAdmin.list_select_related"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ModelAdmin.list_select_related</span></code></a>
does.</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.get_search_fields">
<code class="descclassname">ModelAdmin.</code><code class="descname">get_search_fields</code>(<em>request</em>)<a class="reference internal" href="../../../_modules/django/contrib/admin/options.html#ModelAdmin.get_search_fields"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.contrib.admin.ModelAdmin.get_search_fields" title="Permalink to this definition">¶</a></dt>
<dd><p>The <code class="docutils literal notranslate"><span class="pre">get_search_fields</span></code> method is given the <code class="docutils literal notranslate"><span class="pre">HttpRequest</span></code> and is expected
to return the same kind of sequence type as for the
<a class="reference internal" href="#django.contrib.admin.ModelAdmin.search_fields" title="django.contrib.admin.ModelAdmin.search_fields"><code class="xref py py-attr docutils literal notranslate"><span class="pre">search_fields</span></code></a> attribute.</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.get_inline_instances">
<code class="descclassname">ModelAdmin.</code><code class="descname">get_inline_instances</code>(<em>request</em>, <em>obj=None</em>)<a class="reference internal" href="../../../_modules/django/contrib/admin/options.html#ModelAdmin.get_inline_instances"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.contrib.admin.ModelAdmin.get_inline_instances" title="Permalink to this definition">¶</a></dt>
<dd><p>The <code class="docutils literal notranslate"><span class="pre">get_inline_instances</span></code> method is given the <code class="docutils literal notranslate"><span class="pre">HttpRequest</span></code> and the
<code class="docutils literal notranslate"><span class="pre">obj</span></code> being edited (or <code class="docutils literal notranslate"><span class="pre">None</span></code> on an add form) and is expected to return
a <code class="docutils literal notranslate"><span class="pre">list</span></code> or <code class="docutils literal notranslate"><span class="pre">tuple</span></code> of <a class="reference internal" href="#django.contrib.admin.InlineModelAdmin" title="django.contrib.admin.InlineModelAdmin"><code class="xref py py-class docutils literal notranslate"><span class="pre">InlineModelAdmin</span></code></a>
objects, as described below in the <a class="reference internal" href="#django.contrib.admin.InlineModelAdmin" title="django.contrib.admin.InlineModelAdmin"><code class="xref py py-class docutils literal notranslate"><span class="pre">InlineModelAdmin</span></code></a>
section. For example, the following would return inlines without the default
filtering based on add, change, and delete permissions:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">MyModelAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">inlines</span> <span class="o">=</span> <span class="p">(</span><span class="n">MyInline</span><span class="p">,)</span>

    <span class="k">def</span> <span class="nf">get_inline_instances</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">[</span><span class="n">inline</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">admin_site</span><span class="p">)</span> <span class="k">for</span> <span class="n">inline</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">inlines</span><span class="p">]</span>
</pre></div>
</div>
<p>If you override this method, make sure that the returned inlines are
instances of the classes defined in <a class="reference internal" href="#django.contrib.admin.ModelAdmin.inlines" title="django.contrib.admin.ModelAdmin.inlines"><code class="xref py py-attr docutils literal notranslate"><span class="pre">inlines</span></code></a> or you might encounter
a “Bad Request” error when adding related objects.</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.get_urls">
<code class="descclassname">ModelAdmin.</code><code class="descname">get_urls</code>()<a class="reference internal" href="../../../_modules/django/contrib/admin/options.html#ModelAdmin.get_urls"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.contrib.admin.ModelAdmin.get_urls" title="Permalink to this definition">¶</a></dt>
<dd><p>The <code class="docutils literal notranslate"><span class="pre">get_urls</span></code> method on a <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> returns the URLs to be used for
that ModelAdmin in the same way as a URLconf.  Therefore you can extend
them as documented in <a class="reference internal" href="../../../topics/http/urls.html"><span class="doc">URL dispatcher</span></a>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">MyModelAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">get_urls</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">urls</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">MyModelAdmin</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">get_urls</span><span class="p">()</span>
        <span class="n">my_urls</span> <span class="o">=</span> <span class="p">[</span>
            <span class="n">url</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^my_view/$&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">my_view</span><span class="p">),</span>
        <span class="p">]</span>
        <span class="k">return</span> <span class="n">my_urls</span> <span class="o">+</span> <span class="n">urls</span>

    <span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">):</span>
        <span class="c1"># ...</span>
        <span class="n">context</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span>
           <span class="c1"># Include common variables for rendering the admin template.</span>
           <span class="bp">self</span><span class="o">.</span><span class="n">admin_site</span><span class="o">.</span><span class="n">each_context</span><span class="p">(</span><span class="n">request</span><span class="p">),</span>
           <span class="c1"># Anything else you want in the context...</span>
           <span class="n">key</span><span class="o">=</span><span class="n">value</span><span class="p">,</span>
        <span class="p">)</span>
        <span class="k">return</span> <span class="n">TemplateResponse</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="s2">&quot;sometemplate.html&quot;</span><span class="p">,</span> <span class="n">context</span><span class="p">)</span>
</pre></div>
</div>
<p>If you want to use the admin layout, extend from <code class="docutils literal notranslate"><span class="pre">admin/base_site.html</span></code>:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">extends</span> <span class="s2">&quot;admin/base_site.html&quot;</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">block</span> <span class="nv">content</span> <span class="cp">%}</span>
...
<span class="cp">{%</span> <span class="k">endblock</span> <span class="cp">%}</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p>Notice that the custom patterns are included <em>before</em> the regular admin
URLs: the admin URL patterns are very permissive and will match nearly
anything, so you’ll usually want to prepend your custom URLs to the
built-in ones.</p>
<p class="last">In this example, <code class="docutils literal notranslate"><span class="pre">my_view</span></code> will be accessed at
<code class="docutils literal notranslate"><span class="pre">/admin/myapp/mymodel/my_view/</span></code> (assuming the admin URLs are included
at <code class="docutils literal notranslate"><span class="pre">/admin/</span></code>.)</p>
</div>
<p>However, the <code class="docutils literal notranslate"><span class="pre">self.my_view</span></code> function registered above suffers from two
problems:</p>
<ul class="simple">
<li>It will <em>not</em> perform any permission checks, so it will be accessible
to the general public.</li>
<li>It will <em>not</em> provide any header details to prevent caching. This means
if the page retrieves data from the database, and caching middleware is
active, the page could show outdated information.</li>
</ul>
<p>Since this is usually not what you want, Django provides a convenience
wrapper to check permissions and mark the view as non-cacheable. This
wrapper is <code class="docutils literal notranslate"><span class="pre">AdminSite.admin_view()</span></code> (i.e. <code class="docutils literal notranslate"><span class="pre">self.admin_site.admin_view</span></code>
inside a <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> instance); use it like so:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">MyModelAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">get_urls</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">urls</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">MyModelAdmin</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">get_urls</span><span class="p">()</span>
        <span class="n">my_urls</span> <span class="o">=</span> <span class="p">[</span>
            <span class="n">url</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^my_view/$&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">admin_site</span><span class="o">.</span><span class="n">admin_view</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">my_view</span><span class="p">))</span>
        <span class="p">]</span>
        <span class="k">return</span> <span class="n">my_urls</span> <span class="o">+</span> <span class="n">urls</span>
</pre></div>
</div>
<p>Notice the wrapped view in the fifth line above:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">url</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^my_view/$&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">admin_site</span><span class="o">.</span><span class="n">admin_view</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">my_view</span><span class="p">))</span>
</pre></div>
</div>
<p>This wrapping will protect <code class="docutils literal notranslate"><span class="pre">self.my_view</span></code> from unauthorized access and
will apply the <a class="reference internal" href="../../../topics/http/decorators.html#django.views.decorators.cache.never_cache" title="django.views.decorators.cache.never_cache"><code class="xref py py-func docutils literal notranslate"><span class="pre">django.views.decorators.cache.never_cache()</span></code></a> decorator to
make sure it is not cached if the cache middleware is active.</p>
<p>If the page is cacheable, but you still want the permission check to be
performed, you can pass a <code class="docutils literal notranslate"><span class="pre">cacheable=True</span></code> argument to
<code class="docutils literal notranslate"><span class="pre">AdminSite.admin_view()</span></code>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">url</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^my_view/$&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">admin_site</span><span class="o">.</span><span class="n">admin_view</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">my_view</span><span class="p">,</span> <span class="n">cacheable</span><span class="o">=</span><span class="kc">True</span><span class="p">))</span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> views have <code class="docutils literal notranslate"><span class="pre">model_admin</span></code> attributes. Other
<code class="docutils literal notranslate"><span class="pre">AdminSite</span></code> views have <code class="docutils literal notranslate"><span class="pre">admin_site</span></code> attributes.</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.get_form">
<code class="descclassname">ModelAdmin.</code><code class="descname">get_form</code>(<em>request</em>, <em>obj=None</em>, <em>**kwargs</em>)<a class="reference internal" href="../../../_modules/django/contrib/admin/options.html#ModelAdmin.get_form"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.contrib.admin.ModelAdmin.get_form" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a <a class="reference internal" href="../../../topics/forms/modelforms.html#django.forms.ModelForm" title="django.forms.ModelForm"><code class="xref py py-class docutils literal notranslate"><span class="pre">ModelForm</span></code></a> class for use in the admin add
and change views, see <a class="reference internal" href="#django.contrib.admin.ModelAdmin.add_view" title="django.contrib.admin.ModelAdmin.add_view"><code class="xref py py-meth docutils literal notranslate"><span class="pre">add_view()</span></code></a> and <a class="reference internal" href="#django.contrib.admin.ModelAdmin.change_view" title="django.contrib.admin.ModelAdmin.change_view"><code class="xref py py-meth docutils literal notranslate"><span class="pre">change_view()</span></code></a>.</p>
<p>The base implementation uses <a class="reference internal" href="../../forms/models.html#django.forms.models.modelform_factory" title="django.forms.models.modelform_factory"><code class="xref py py-func docutils literal notranslate"><span class="pre">modelform_factory()</span></code></a>
to subclass <a class="reference internal" href="#django.contrib.admin.ModelAdmin.form" title="django.contrib.admin.ModelAdmin.form"><code class="xref py py-attr docutils literal notranslate"><span class="pre">form</span></code></a>, modified by attributes such as <a class="reference internal" href="#django.contrib.admin.ModelAdmin.fields" title="django.contrib.admin.ModelAdmin.fields"><code class="xref py py-attr docutils literal notranslate"><span class="pre">fields</span></code></a>
and <a class="reference internal" href="#django.contrib.admin.ModelAdmin.exclude" title="django.contrib.admin.ModelAdmin.exclude"><code class="xref py py-attr docutils literal notranslate"><span class="pre">exclude</span></code></a>. So, for example, if you wanted to offer additional
fields to superusers, you could swap in a different base form like so:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">MyModelAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">get_form</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">request</span><span class="o">.</span><span class="n">user</span><span class="o">.</span><span class="n">is_superuser</span><span class="p">:</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;form&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">MySuperuserForm</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">MyModelAdmin</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">get_form</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
</pre></div>
</div>
<p>You may also simply return a custom <a class="reference internal" href="../../../topics/forms/modelforms.html#django.forms.ModelForm" title="django.forms.ModelForm"><code class="xref py py-class docutils literal notranslate"><span class="pre">ModelForm</span></code></a> class
directly.</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.get_formsets_with_inlines">
<code class="descclassname">ModelAdmin.</code><code class="descname">get_formsets_with_inlines</code>(<em>request</em>, <em>obj=None</em>)<a class="reference internal" href="../../../_modules/django/contrib/admin/options.html#ModelAdmin.get_formsets_with_inlines"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.contrib.admin.ModelAdmin.get_formsets_with_inlines" title="Permalink to this definition">¶</a></dt>
<dd><p>Yields (<code class="docutils literal notranslate"><span class="pre">FormSet</span></code>, <a class="reference internal" href="#django.contrib.admin.InlineModelAdmin" title="django.contrib.admin.InlineModelAdmin"><code class="xref py py-class docutils literal notranslate"><span class="pre">InlineModelAdmin</span></code></a>) pairs for use in admin add
and change views.</p>
<p>For example if you wanted to display a particular inline only in the change
view, you could override <code class="docutils literal notranslate"><span class="pre">get_formsets_with_inlines</span></code> as follows:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">MyModelAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">inlines</span> <span class="o">=</span> <span class="p">[</span><span class="n">MyInline</span><span class="p">,</span> <span class="n">SomeOtherInline</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">get_formsets_with_inlines</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">inline</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_inline_instances</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
            <span class="c1"># hide MyInline in the add view</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">inline</span><span class="p">,</span> <span class="n">MyInline</span><span class="p">)</span> <span class="ow">and</span> <span class="n">obj</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="k">continue</span>
            <span class="k">yield</span> <span class="n">inline</span><span class="o">.</span><span class="n">get_formset</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">),</span> <span class="n">inline</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.formfield_for_foreignkey">
<code class="descclassname">ModelAdmin.</code><code class="descname">formfield_for_foreignkey</code>(<em>db_field</em>, <em>request</em>, <em>**kwargs</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.formfield_for_foreignkey" title="Permalink to this definition">¶</a></dt>
<dd><p>The <code class="docutils literal notranslate"><span class="pre">formfield_for_foreignkey</span></code> method on a <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> allows you to
override the default formfield for a foreign keys field. For example, to
return a subset of objects for this foreign key field based on the user:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">MyModelAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">formfield_for_foreignkey</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">db_field</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">db_field</span><span class="o">.</span><span class="n">name</span> <span class="o">==</span> <span class="s2">&quot;car&quot;</span><span class="p">:</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s2">&quot;queryset&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">Car</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">owner</span><span class="o">=</span><span class="n">request</span><span class="o">.</span><span class="n">user</span><span class="p">)</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">MyModelAdmin</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">formfield_for_foreignkey</span><span class="p">(</span><span class="n">db_field</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
</pre></div>
</div>
<p>This uses the <code class="docutils literal notranslate"><span class="pre">HttpRequest</span></code> instance to filter the <code class="docutils literal notranslate"><span class="pre">Car</span></code> foreign key
field to only display the cars owned by the <code class="docutils literal notranslate"><span class="pre">User</span></code> instance.</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.formfield_for_manytomany">
<code class="descclassname">ModelAdmin.</code><code class="descname">formfield_for_manytomany</code>(<em>db_field</em>, <em>request</em>, <em>**kwargs</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.formfield_for_manytomany" title="Permalink to this definition">¶</a></dt>
<dd><p>Like the <code class="docutils literal notranslate"><span class="pre">formfield_for_foreignkey</span></code> method, the
<code class="docutils literal notranslate"><span class="pre">formfield_for_manytomany</span></code> method can be overridden to change the
default formfield for a many to many field. For example, if an owner can
own multiple cars and cars can belong to multiple owners – a many to
many relationship – you could filter the <code class="docutils literal notranslate"><span class="pre">Car</span></code> foreign key field to
only display the cars owned by the <code class="docutils literal notranslate"><span class="pre">User</span></code>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">MyModelAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">formfield_for_manytomany</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">db_field</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">db_field</span><span class="o">.</span><span class="n">name</span> <span class="o">==</span> <span class="s2">&quot;cars&quot;</span><span class="p">:</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s2">&quot;queryset&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">Car</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">owner</span><span class="o">=</span><span class="n">request</span><span class="o">.</span><span class="n">user</span><span class="p">)</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">MyModelAdmin</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">formfield_for_manytomany</span><span class="p">(</span><span class="n">db_field</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.formfield_for_choice_field">
<code class="descclassname">ModelAdmin.</code><code class="descname">formfield_for_choice_field</code>(<em>db_field</em>, <em>request</em>, <em>**kwargs</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.formfield_for_choice_field" title="Permalink to this definition">¶</a></dt>
<dd><p>Like the <code class="docutils literal notranslate"><span class="pre">formfield_for_foreignkey</span></code> and <code class="docutils literal notranslate"><span class="pre">formfield_for_manytomany</span></code>
methods, the <code class="docutils literal notranslate"><span class="pre">formfield_for_choice_field</span></code> method can be overridden to
change the default formfield for a field that has declared choices. For
example, if the choices available to a superuser should be different than
those available to regular staff, you could proceed as follows:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">MyModelAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">formfield_for_choice_field</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">db_field</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">db_field</span><span class="o">.</span><span class="n">name</span> <span class="o">==</span> <span class="s2">&quot;status&quot;</span><span class="p">:</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;choices&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span>
                <span class="p">(</span><span class="s1">&#39;accepted&#39;</span><span class="p">,</span> <span class="s1">&#39;Accepted&#39;</span><span class="p">),</span>
                <span class="p">(</span><span class="s1">&#39;denied&#39;</span><span class="p">,</span> <span class="s1">&#39;Denied&#39;</span><span class="p">),</span>
            <span class="p">)</span>
            <span class="k">if</span> <span class="n">request</span><span class="o">.</span><span class="n">user</span><span class="o">.</span><span class="n">is_superuser</span><span class="p">:</span>
                <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;choices&#39;</span><span class="p">]</span> <span class="o">+=</span> <span class="p">((</span><span class="s1">&#39;ready&#39;</span><span class="p">,</span> <span class="s1">&#39;Ready for deployment&#39;</span><span class="p">),)</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">MyModelAdmin</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">formfield_for_choice_field</span><span class="p">(</span><span class="n">db_field</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
</pre></div>
</div>
<div class="admonition-note admonition">
<p class="first admonition-title">Note</p>
<p class="last">Any <code class="docutils literal notranslate"><span class="pre">choices</span></code> attribute set on the formfield will be limited to the
form field only. If the corresponding field on the model has choices
set, the choices provided to the form must be a valid subset of those
choices, otherwise the form submission will fail with
a <a class="reference internal" href="../../exceptions.html#django.core.exceptions.ValidationError" title="django.core.exceptions.ValidationError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValidationError</span></code></a> when the model itself
is validated before saving.</p>
</div>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.get_changelist">
<code class="descclassname">ModelAdmin.</code><code class="descname">get_changelist</code>(<em>request</em>, <em>**kwargs</em>)<a class="reference internal" href="../../../_modules/django/contrib/admin/options.html#ModelAdmin.get_changelist"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.contrib.admin.ModelAdmin.get_changelist" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the <code class="docutils literal notranslate"><span class="pre">Changelist</span></code> class to be used for listing. By default,
<code class="docutils literal notranslate"><span class="pre">django.contrib.admin.views.main.ChangeList</span></code> is used. By inheriting this
class you can change the behavior of the listing.</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.get_changelist_form">
<code class="descclassname">ModelAdmin.</code><code class="descname">get_changelist_form</code>(<em>request</em>, <em>**kwargs</em>)<a class="reference internal" href="../../../_modules/django/contrib/admin/options.html#ModelAdmin.get_changelist_form"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.contrib.admin.ModelAdmin.get_changelist_form" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a <a class="reference internal" href="../../../topics/forms/modelforms.html#django.forms.ModelForm" title="django.forms.ModelForm"><code class="xref py py-class docutils literal notranslate"><span class="pre">ModelForm</span></code></a> class for use in the <code class="docutils literal notranslate"><span class="pre">Formset</span></code>
on the changelist page. To use a custom form, for example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django</span> <span class="k">import</span> <span class="n">forms</span>

<span class="k">class</span> <span class="nc">MyForm</span><span class="p">(</span><span class="n">forms</span><span class="o">.</span><span class="n">ModelForm</span><span class="p">):</span>
    <span class="k">pass</span>

<span class="k">class</span> <span class="nc">MyModelAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">get_changelist_form</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">MyForm</span>
</pre></div>
</div>
<div class="admonition-note admonition">
<p class="first admonition-title">Note</p>
<p class="last">If you define the <code class="docutils literal notranslate"><span class="pre">Meta.model</span></code> attribute on a
<a class="reference internal" href="../../../topics/forms/modelforms.html#django.forms.ModelForm" title="django.forms.ModelForm"><code class="xref py py-class docutils literal notranslate"><span class="pre">ModelForm</span></code></a>, you must also define the
<code class="docutils literal notranslate"><span class="pre">Meta.fields</span></code> attribute (or the <code class="docutils literal notranslate"><span class="pre">Meta.exclude</span></code> attribute). However,
<code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> ignores this value, overriding it with the
<a class="reference internal" href="#django.contrib.admin.ModelAdmin.list_editable" title="django.contrib.admin.ModelAdmin.list_editable"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ModelAdmin.list_editable</span></code></a> attribute. The easiest solution is to
omit the <code class="docutils literal notranslate"><span class="pre">Meta.model</span></code> attribute, since <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> will provide the
correct model to use.</p>
</div>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.get_changelist_formset">
<code class="descclassname">ModelAdmin.</code><code class="descname">get_changelist_formset</code>(<em>request</em>, <em>**kwargs</em>)<a class="reference internal" href="../../../_modules/django/contrib/admin/options.html#ModelAdmin.get_changelist_formset"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.contrib.admin.ModelAdmin.get_changelist_formset" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a <a class="reference internal" href="../../../topics/forms/modelforms.html#model-formsets"><span class="std std-ref">ModelFormSet</span></a> class for use on the
changelist page if <a class="reference internal" href="#django.contrib.admin.ModelAdmin.list_editable" title="django.contrib.admin.ModelAdmin.list_editable"><code class="xref py py-attr docutils literal notranslate"><span class="pre">list_editable</span></code></a> is used. To use a
custom formset, for example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.forms</span> <span class="k">import</span> <span class="n">BaseModelFormSet</span>

<span class="k">class</span> <span class="nc">MyAdminFormSet</span><span class="p">(</span><span class="n">BaseModelFormSet</span><span class="p">):</span>
    <span class="k">pass</span>

<span class="k">class</span> <span class="nc">MyModelAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">get_changelist_formset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;formset&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">MyAdminFormSet</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">MyModelAdmin</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">get_changelist_formset</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.lookup_allowed">
<code class="descclassname">ModelAdmin.</code><code class="descname">lookup_allowed</code>(<em>lookup</em>, <em>value</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.lookup_allowed" title="Permalink to this definition">¶</a></dt>
<dd><p>The objects in the changelist page can be filtered with lookups from the
URL’s query string. This is how <a class="reference internal" href="#django.contrib.admin.ModelAdmin.list_filter" title="django.contrib.admin.ModelAdmin.list_filter"><code class="xref py py-attr docutils literal notranslate"><span class="pre">list_filter</span></code></a> works, for example. The
lookups are similar to what’s used in <a class="reference internal" href="../../models/querysets.html#django.db.models.query.QuerySet.filter" title="django.db.models.query.QuerySet.filter"><code class="xref py py-meth docutils literal notranslate"><span class="pre">QuerySet.filter()</span></code></a> (e.g.
<code class="docutils literal notranslate"><span class="pre">user__email=user&#64;example.com</span></code>). Since the lookups in the query string
can be manipulated by the user, they must be sanitized to prevent
unauthorized data exposure.</p>
<p>The <code class="docutils literal notranslate"><span class="pre">lookup_allowed()</span></code> method is given a lookup path from the query string
(e.g. <code class="docutils literal notranslate"><span class="pre">'user__email'</span></code>) and the corresponding value
(e.g. <code class="docutils literal notranslate"><span class="pre">'user&#64;example.com'</span></code>), and returns a boolean indicating whether
filtering the changelist’s <code class="docutils literal notranslate"><span class="pre">QuerySet</span></code> using the parameters is permitted.
If <code class="docutils literal notranslate"><span class="pre">lookup_allowed()</span></code> returns <code class="docutils literal notranslate"><span class="pre">False</span></code>, <code class="docutils literal notranslate"><span class="pre">DisallowedModelAdminLookup</span></code>
(subclass of <a class="reference internal" href="../../exceptions.html#django.core.exceptions.SuspiciousOperation" title="django.core.exceptions.SuspiciousOperation"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SuspiciousOperation</span></code></a>) is raised.</p>
<p>By default, <code class="docutils literal notranslate"><span class="pre">lookup_allowed()</span></code> allows access to a model’s local fields,
field paths used in <a class="reference internal" href="#django.contrib.admin.ModelAdmin.list_filter" title="django.contrib.admin.ModelAdmin.list_filter"><code class="xref py py-attr docutils literal notranslate"><span class="pre">list_filter</span></code></a> (but not paths from
<a class="reference internal" href="#django.contrib.admin.ModelAdmin.get_list_filter" title="django.contrib.admin.ModelAdmin.get_list_filter"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_list_filter()</span></code></a>), and lookups required for
<a class="reference internal" href="../../models/fields.html#django.db.models.ForeignKey.limit_choices_to" title="django.db.models.ForeignKey.limit_choices_to"><code class="xref py py-attr docutils literal notranslate"><span class="pre">limit_choices_to</span></code></a> to function
correctly in <a class="reference internal" href="#django.contrib.admin.ModelAdmin.raw_id_fields" title="django.contrib.admin.ModelAdmin.raw_id_fields"><code class="xref py py-attr docutils literal notranslate"><span class="pre">raw_id_fields</span></code></a>.</p>
<p>Override this method to customize the lookups permitted for your
<a class="reference internal" href="#django.contrib.admin.ModelAdmin" title="django.contrib.admin.ModelAdmin"><code class="xref py py-class docutils literal notranslate"><span class="pre">ModelAdmin</span></code></a> subclass.</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.has_add_permission">
<code class="descclassname">ModelAdmin.</code><code class="descname">has_add_permission</code>(<em>request</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.has_add_permission" title="Permalink to this definition">¶</a></dt>
<dd><p>Should return <code class="docutils literal notranslate"><span class="pre">True</span></code> if adding an object is permitted, <code class="docutils literal notranslate"><span class="pre">False</span></code>
otherwise.</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.has_change_permission">
<code class="descclassname">ModelAdmin.</code><code class="descname">has_change_permission</code>(<em>request</em>, <em>obj=None</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.has_change_permission" title="Permalink to this definition">¶</a></dt>
<dd><p>Should return <code class="docutils literal notranslate"><span class="pre">True</span></code> if editing obj is permitted, <code class="docutils literal notranslate"><span class="pre">False</span></code> otherwise.
If obj is <code class="docutils literal notranslate"><span class="pre">None</span></code>, should return <code class="docutils literal notranslate"><span class="pre">True</span></code> or <code class="docutils literal notranslate"><span class="pre">False</span></code> to indicate whether
editing of objects of this type is permitted in general (e.g., <code class="docutils literal notranslate"><span class="pre">False</span></code>
will be interpreted as meaning that the current user is not permitted to
edit any object of this type).</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.has_delete_permission">
<code class="descclassname">ModelAdmin.</code><code class="descname">has_delete_permission</code>(<em>request</em>, <em>obj=None</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.has_delete_permission" title="Permalink to this definition">¶</a></dt>
<dd><p>Should return <code class="docutils literal notranslate"><span class="pre">True</span></code> if deleting obj is permitted, <code class="docutils literal notranslate"><span class="pre">False</span></code> otherwise.
If obj is <code class="docutils literal notranslate"><span class="pre">None</span></code>, should return <code class="docutils literal notranslate"><span class="pre">True</span></code> or <code class="docutils literal notranslate"><span class="pre">False</span></code> to indicate whether
deleting objects of this type is permitted in general (e.g., <code class="docutils literal notranslate"><span class="pre">False</span></code> will
be interpreted as meaning that the current user is not permitted to delete
any object of this type).</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.has_module_permission">
<code class="descclassname">ModelAdmin.</code><code class="descname">has_module_permission</code>(<em>request</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.has_module_permission" title="Permalink to this definition">¶</a></dt>
<dd><p>Should return <code class="docutils literal notranslate"><span class="pre">True</span></code> if displaying the module on the admin index page and
accessing the module’s index page is permitted, <code class="docutils literal notranslate"><span class="pre">False</span></code> otherwise.
Uses <a class="reference internal" href="../auth.html#django.contrib.auth.models.User.has_module_perms" title="django.contrib.auth.models.User.has_module_perms"><code class="xref py py-meth docutils literal notranslate"><span class="pre">User.has_module_perms()</span></code></a> by default. Overriding
it does not restrict access to the add, change or delete views,
<a class="reference internal" href="#django.contrib.admin.ModelAdmin.has_add_permission" title="django.contrib.admin.ModelAdmin.has_add_permission"><code class="xref py py-meth docutils literal notranslate"><span class="pre">has_add_permission()</span></code></a>,
<a class="reference internal" href="#django.contrib.admin.ModelAdmin.has_change_permission" title="django.contrib.admin.ModelAdmin.has_change_permission"><code class="xref py py-meth docutils literal notranslate"><span class="pre">has_change_permission()</span></code></a>, and
<a class="reference internal" href="#django.contrib.admin.ModelAdmin.has_delete_permission" title="django.contrib.admin.ModelAdmin.has_delete_permission"><code class="xref py py-meth docutils literal notranslate"><span class="pre">has_delete_permission()</span></code></a> should be used for that.</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.get_queryset">
<code class="descclassname">ModelAdmin.</code><code class="descname">get_queryset</code>(<em>request</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.get_queryset" title="Permalink to this definition">¶</a></dt>
<dd><p>The <code class="docutils literal notranslate"><span class="pre">get_queryset</span></code> method on a <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> returns a
<a class="reference internal" href="../../models/querysets.html#django.db.models.query.QuerySet" title="django.db.models.query.QuerySet"><code class="xref py py-class docutils literal notranslate"><span class="pre">QuerySet</span></code></a> of all model instances that
can be edited by the admin site. One use case for overriding this method
is to show objects owned by the logged-in user:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">MyModelAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">get_queryset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">):</span>
        <span class="n">qs</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">MyModelAdmin</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">get_queryset</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">request</span><span class="o">.</span><span class="n">user</span><span class="o">.</span><span class="n">is_superuser</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">qs</span>
        <span class="k">return</span> <span class="n">qs</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">author</span><span class="o">=</span><span class="n">request</span><span class="o">.</span><span class="n">user</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.message_user">
<code class="descclassname">ModelAdmin.</code><code class="descname">message_user</code>(<em>request</em>, <em>message</em>, <em>level=messages.INFO</em>, <em>extra_tags=''</em>, <em>fail_silently=False</em>)<a class="reference internal" href="../../../_modules/django/contrib/admin/options.html#ModelAdmin.message_user"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.contrib.admin.ModelAdmin.message_user" title="Permalink to this definition">¶</a></dt>
<dd><p>Sends a message to the user using the <a class="reference internal" href="../messages.html#module-django.contrib.messages" title="django.contrib.messages: Provides cookie- and session-based temporary message storage."><code class="xref py py-mod docutils literal notranslate"><span class="pre">django.contrib.messages</span></code></a>
backend.  See the <a class="reference internal" href="actions.html#custom-admin-action"><span class="std std-ref">custom ModelAdmin example</span></a>.</p>
<p>Keyword arguments allow you to change the message level, add extra CSS
tags, or fail silently if the <code class="docutils literal notranslate"><span class="pre">contrib.messages</span></code> framework is not
installed. These keyword arguments match those for
<a class="reference internal" href="../messages.html#django.contrib.messages.add_message" title="django.contrib.messages.add_message"><code class="xref py py-func docutils literal notranslate"><span class="pre">django.contrib.messages.add_message()</span></code></a>, see that function’s
documentation for more details. One difference is that the level may be
passed as a string label in addition to integer/constant.</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.get_paginator">
<code class="descclassname">ModelAdmin.</code><code class="descname">get_paginator</code>(<em>request</em>, <em>queryset</em>, <em>per_page</em>, <em>orphans=0</em>, <em>allow_empty_first_page=True</em>)<a class="reference internal" href="../../../_modules/django/contrib/admin/options.html#ModelAdmin.get_paginator"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.contrib.admin.ModelAdmin.get_paginator" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns an instance of the paginator to use for this view. By default,
instantiates an instance of <a class="reference internal" href="#django.contrib.admin.ModelAdmin.paginator" title="django.contrib.admin.ModelAdmin.paginator"><code class="xref py py-attr docutils literal notranslate"><span class="pre">paginator</span></code></a>.</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.response_add">
<code class="descclassname">ModelAdmin.</code><code class="descname">response_add</code>(<em>request</em>, <em>obj</em>, <em>post_url_continue=None</em>)<a class="reference internal" href="../../../_modules/django/contrib/admin/options.html#ModelAdmin.response_add"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.contrib.admin.ModelAdmin.response_add" title="Permalink to this definition">¶</a></dt>
<dd><p>Determines the <a class="reference internal" href="../../request-response.html#django.http.HttpResponse" title="django.http.HttpResponse"><code class="xref py py-class docutils literal notranslate"><span class="pre">HttpResponse</span></code></a> for the
<a class="reference internal" href="#django.contrib.admin.ModelAdmin.add_view" title="django.contrib.admin.ModelAdmin.add_view"><code class="xref py py-meth docutils literal notranslate"><span class="pre">add_view()</span></code></a> stage.</p>
<p><code class="docutils literal notranslate"><span class="pre">response_add</span></code> is called after the admin form is submitted and
just after the object and all the related instances have
been created and saved. You can override it to change the default behavior
after the object has been created.</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.response_change">
<code class="descclassname">ModelAdmin.</code><code class="descname">response_change</code>(<em>request</em>, <em>obj</em>)<a class="reference internal" href="../../../_modules/django/contrib/admin/options.html#ModelAdmin.response_change"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.contrib.admin.ModelAdmin.response_change" title="Permalink to this definition">¶</a></dt>
<dd><p>Determines the <a class="reference internal" href="../../request-response.html#django.http.HttpResponse" title="django.http.HttpResponse"><code class="xref py py-class docutils literal notranslate"><span class="pre">HttpResponse</span></code></a> for the
<a class="reference internal" href="#django.contrib.admin.ModelAdmin.change_view" title="django.contrib.admin.ModelAdmin.change_view"><code class="xref py py-meth docutils literal notranslate"><span class="pre">change_view()</span></code></a> stage.</p>
<p><code class="docutils literal notranslate"><span class="pre">response_change</span></code> is called after the admin form is submitted and
just after the object and all the related instances have
been saved. You can override it to change the default
behavior after the object has been changed.</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.response_delete">
<code class="descclassname">ModelAdmin.</code><code class="descname">response_delete</code>(<em>request</em>, <em>obj_display</em>, <em>obj_id</em>)<a class="reference internal" href="../../../_modules/django/contrib/admin/options.html#ModelAdmin.response_delete"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.contrib.admin.ModelAdmin.response_delete" title="Permalink to this definition">¶</a></dt>
<dd><p>Determines the <a class="reference internal" href="../../request-response.html#django.http.HttpResponse" title="django.http.HttpResponse"><code class="xref py py-class docutils literal notranslate"><span class="pre">HttpResponse</span></code></a> for the
<a class="reference internal" href="#django.contrib.admin.ModelAdmin.delete_view" title="django.contrib.admin.ModelAdmin.delete_view"><code class="xref py py-meth docutils literal notranslate"><span class="pre">delete_view()</span></code></a> stage.</p>
<p><code class="docutils literal notranslate"><span class="pre">response_delete</span></code> is called after the object has been
deleted. You can override it to change the default
behavior after the object has been deleted.</p>
<p><code class="docutils literal notranslate"><span class="pre">obj_display</span></code> is a string with the name of the deleted
object.</p>
<p><code class="docutils literal notranslate"><span class="pre">obj_id</span></code> is the serialized identifier used to retrieve the object to be
deleted.</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.get_changeform_initial_data">
<code class="descclassname">ModelAdmin.</code><code class="descname">get_changeform_initial_data</code>(<em>request</em>)<a class="reference internal" href="../../../_modules/django/contrib/admin/options.html#ModelAdmin.get_changeform_initial_data"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.contrib.admin.ModelAdmin.get_changeform_initial_data" title="Permalink to this definition">¶</a></dt>
<dd><p>A hook for the initial data on admin change forms. By default, fields are
given initial values from <code class="docutils literal notranslate"><span class="pre">GET</span></code> parameters. For instance,
<code class="docutils literal notranslate"><span class="pre">?name=initial_value</span></code> will set the <code class="docutils literal notranslate"><span class="pre">name</span></code> field’s initial value to be
<code class="docutils literal notranslate"><span class="pre">initial_value</span></code>.</p>
<p>This method should return a dictionary in the form
<code class="docutils literal notranslate"><span class="pre">{'fieldname':</span> <span class="pre">'fieldval'}</span></code>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">get_changeform_initial_data</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">):</span>
    <span class="k">return</span> <span class="p">{</span><span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="s1">&#39;custom_initial_value&#39;</span><span class="p">}</span>
</pre></div>
</div>
</dd></dl>

<div class="section" id="s-other-methods">
<span id="other-methods"></span><h4>Other methods<a class="headerlink" href="#other-methods" title="Permalink to this headline">¶</a></h4>
<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.add_view">
<code class="descclassname">ModelAdmin.</code><code class="descname">add_view</code>(<em>request</em>, <em>form_url=''</em>, <em>extra_context=None</em>)<a class="reference internal" href="../../../_modules/django/contrib/admin/options.html#ModelAdmin.add_view"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.contrib.admin.ModelAdmin.add_view" title="Permalink to this definition">¶</a></dt>
<dd><p>Django view for the model instance addition page. See note below.</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.change_view">
<code class="descclassname">ModelAdmin.</code><code class="descname">change_view</code>(<em>request</em>, <em>object_id</em>, <em>form_url=''</em>, <em>extra_context=None</em>)<a class="reference internal" href="../../../_modules/django/contrib/admin/options.html#ModelAdmin.change_view"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.contrib.admin.ModelAdmin.change_view" title="Permalink to this definition">¶</a></dt>
<dd><p>Django view for the model instance editing page. See note below.</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.changelist_view">
<code class="descclassname">ModelAdmin.</code><code class="descname">changelist_view</code>(<em>request</em>, <em>extra_context=None</em>)<a class="reference internal" href="../../../_modules/django/contrib/admin/options.html#ModelAdmin.changelist_view"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.contrib.admin.ModelAdmin.changelist_view" title="Permalink to this definition">¶</a></dt>
<dd><p>Django view for the model instances change list/actions page. See note
below.</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.delete_view">
<code class="descclassname">ModelAdmin.</code><code class="descname">delete_view</code>(<em>request</em>, <em>object_id</em>, <em>extra_context=None</em>)<a class="reference internal" href="../../../_modules/django/contrib/admin/options.html#ModelAdmin.delete_view"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.contrib.admin.ModelAdmin.delete_view" title="Permalink to this definition">¶</a></dt>
<dd><p>Django view for the model instance(s) deletion confirmation page. See note
below.</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.history_view">
<code class="descclassname">ModelAdmin.</code><code class="descname">history_view</code>(<em>request</em>, <em>object_id</em>, <em>extra_context=None</em>)<a class="reference internal" href="../../../_modules/django/contrib/admin/options.html#ModelAdmin.history_view"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.contrib.admin.ModelAdmin.history_view" title="Permalink to this definition">¶</a></dt>
<dd><p>Django view for the page that shows the modification history for a given
model instance.</p>
</dd></dl>

<p>Unlike the hook-type <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> methods detailed in the previous section,
these five methods are in reality designed to be invoked as Django views from
the admin application URL dispatching handler to render the pages that deal
with model instances CRUD operations. As a result, completely overriding these
methods will significantly change the behavior of the admin application.</p>
<p>One common reason for overriding these methods is to augment the context data
that is provided to the template that renders the view. In the following
example, the change view is overridden so that the rendered template is
provided some extra mapping data that would not otherwise be available:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">MyModelAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>

    <span class="c1"># A template for a very customized change view:</span>
    <span class="n">change_form_template</span> <span class="o">=</span> <span class="s1">&#39;admin/myapp/extras/openstreetmap_change_form.html&#39;</span>

    <span class="k">def</span> <span class="nf">get_osm_info</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># ...</span>
        <span class="k">pass</span>

    <span class="k">def</span> <span class="nf">change_view</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">object_id</span><span class="p">,</span> <span class="n">form_url</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">extra_context</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="n">extra_context</span> <span class="o">=</span> <span class="n">extra_context</span> <span class="ow">or</span> <span class="p">{}</span>
        <span class="n">extra_context</span><span class="p">[</span><span class="s1">&#39;osm_data&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_osm_info</span><span class="p">()</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">MyModelAdmin</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">change_view</span><span class="p">(</span>
            <span class="n">request</span><span class="p">,</span> <span class="n">object_id</span><span class="p">,</span> <span class="n">form_url</span><span class="p">,</span> <span class="n">extra_context</span><span class="o">=</span><span class="n">extra_context</span><span class="p">,</span>
        <span class="p">)</span>
</pre></div>
</div>
<p>These views return <a class="reference internal" href="../../template-response.html#django.template.response.TemplateResponse" title="django.template.response.TemplateResponse"><code class="xref py py-class docutils literal notranslate"><span class="pre">TemplateResponse</span></code></a>
instances which allow you to easily customize the response data before
rendering. For more details, see the <a class="reference internal" href="../../template-response.html"><span class="doc">TemplateResponse documentation</span></a>.</p>
</div>
</div>
<div class="section" id="s-modeladmin-asset-definitions">
<span id="s-id1"></span><span id="modeladmin-asset-definitions"></span><span id="id1"></span><h3><code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> asset definitions<a class="headerlink" href="#modeladmin-asset-definitions" title="Permalink to this headline">¶</a></h3>
<p>There are times where you would like add a bit of CSS and/or JavaScript to
the add/change views. This can be accomplished by using a <code class="docutils literal notranslate"><span class="pre">Media</span></code> inner class
on your <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">ArticleAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="k">class</span> <span class="nc">Media</span><span class="p">:</span>
        <span class="n">css</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s2">&quot;all&quot;</span><span class="p">:</span> <span class="p">(</span><span class="s2">&quot;my_styles.css&quot;</span><span class="p">,)</span>
        <span class="p">}</span>
        <span class="n">js</span> <span class="o">=</span> <span class="p">(</span><span class="s2">&quot;my_code.js&quot;</span><span class="p">,)</span>
</pre></div>
</div>
<p>The <a class="reference internal" href="../staticfiles.html"><span class="doc">staticfiles app</span></a> prepends
<a class="reference internal" href="../../settings.html#std:setting-STATIC_URL"><code class="xref std std-setting docutils literal notranslate"><span class="pre">STATIC_URL</span></code></a> (or <a class="reference internal" href="../../settings.html#std:setting-MEDIA_URL"><code class="xref std std-setting docutils literal notranslate"><span class="pre">MEDIA_URL</span></code></a> if <a class="reference internal" href="../../settings.html#std:setting-STATIC_URL"><code class="xref std std-setting docutils literal notranslate"><span class="pre">STATIC_URL</span></code></a> is
<code class="docutils literal notranslate"><span class="pre">None</span></code>) to any asset paths. The same rules apply as <a class="reference internal" href="../../../topics/forms/media.html#form-asset-paths"><span class="std std-ref">regular asset
definitions on forms</span></a>.</p>
<div class="section" id="s-jquery">
<span id="s-contrib-admin-jquery"></span><span id="jquery"></span><span id="contrib-admin-jquery"></span><h4>jQuery<a class="headerlink" href="#jquery" title="Permalink to this headline">¶</a></h4>
<p>Django admin JavaScript makes use of the <a class="reference external" href="https://jquery.com">jQuery</a> library.</p>
<p>To avoid conflicts with user-supplied scripts or libraries, Django’s jQuery
(version 2.2.3) is namespaced as <code class="docutils literal notranslate"><span class="pre">django.jQuery</span></code>. If you want to use jQuery
in your own admin JavaScript without including a second copy, you can use the
<code class="docutils literal notranslate"><span class="pre">django.jQuery</span></code> object on changelist and add/edit views.</p>
<div class="versionchanged">
<span class="title">Changed in Django 1.10:</span> <p>The embedded jQuery was upgraded from 2.1.4 to 2.2.3.</p>
</div>
<p>The <a class="reference internal" href="#django.contrib.admin.ModelAdmin" title="django.contrib.admin.ModelAdmin"><code class="xref py py-class docutils literal notranslate"><span class="pre">ModelAdmin</span></code></a> class requires jQuery by default, so there is no need
to add jQuery to your <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code>’s list of media resources unless you have
a specific need. For example, if you require the jQuery library to be in the
global namespace (for example when using third-party jQuery plugins) or if you
need a newer version of jQuery, you will have to include your own copy.</p>
<p>Django provides both uncompressed and ‘minified’ versions of jQuery, as
<code class="docutils literal notranslate"><span class="pre">jquery.js</span></code> and <code class="docutils literal notranslate"><span class="pre">jquery.min.js</span></code> respectively.</p>
<p><a class="reference internal" href="#django.contrib.admin.ModelAdmin" title="django.contrib.admin.ModelAdmin"><code class="xref py py-class docutils literal notranslate"><span class="pre">ModelAdmin</span></code></a> and <a class="reference internal" href="#django.contrib.admin.InlineModelAdmin" title="django.contrib.admin.InlineModelAdmin"><code class="xref py py-class docutils literal notranslate"><span class="pre">InlineModelAdmin</span></code></a> have a <code class="docutils literal notranslate"><span class="pre">media</span></code> property
that returns a list of <code class="docutils literal notranslate"><span class="pre">Media</span></code> objects which store paths to the JavaScript
files for the forms and/or formsets. If <a class="reference internal" href="../../settings.html#std:setting-DEBUG"><code class="xref std std-setting docutils literal notranslate"><span class="pre">DEBUG</span></code></a> is <code class="docutils literal notranslate"><span class="pre">True</span></code> it will
return the uncompressed versions of the various JavaScript files, including
<code class="docutils literal notranslate"><span class="pre">jquery.js</span></code>; if not, it will return the ‘minified’ versions.</p>
</div>
</div>
<div class="section" id="s-adding-custom-validation-to-the-admin">
<span id="s-admin-custom-validation"></span><span id="adding-custom-validation-to-the-admin"></span><span id="admin-custom-validation"></span><h3>Adding custom validation to the admin<a class="headerlink" href="#adding-custom-validation-to-the-admin" title="Permalink to this headline">¶</a></h3>
<p>Adding custom validation of data in the admin is quite easy. The automatic
admin interface reuses <a class="reference internal" href="../../forms/api.html#module-django.forms" title="django.forms"><code class="xref py py-mod docutils literal notranslate"><span class="pre">django.forms</span></code></a>, and the <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> class gives
you the ability define your own form:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">ArticleAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">form</span> <span class="o">=</span> <span class="n">MyArticleAdminForm</span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">MyArticleAdminForm</span></code> can be defined anywhere as long as you import where
needed. Now within your form you can add your own custom validation for
any field:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">MyArticleAdminForm</span><span class="p">(</span><span class="n">forms</span><span class="o">.</span><span class="n">ModelForm</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">clean_name</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># do something that validates your data</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">cleaned_data</span><span class="p">[</span><span class="s2">&quot;name&quot;</span><span class="p">]</span>
</pre></div>
</div>
<p>It is important you use a <code class="docutils literal notranslate"><span class="pre">ModelForm</span></code> here otherwise things can break. See
the <a class="reference internal" href="../../forms/index.html"><span class="doc">forms</span></a> documentation on <a class="reference internal" href="../../forms/validation.html"><span class="doc">custom validation</span></a> and, more specifically, the
<a class="reference internal" href="../../../topics/forms/modelforms.html#overriding-modelform-clean-method"><span class="std std-ref">model form validation notes</span></a> for more
information.</p>
</div>
</div>
<div class="section" id="s-inlinemodeladmin-objects">
<span id="s-admin-inlines"></span><span id="inlinemodeladmin-objects"></span><span id="admin-inlines"></span><h2><code class="docutils literal notranslate"><span class="pre">InlineModelAdmin</span></code> objects<a class="headerlink" href="#inlinemodeladmin-objects" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="django.contrib.admin.InlineModelAdmin">
<em class="property">class </em><code class="descname">InlineModelAdmin</code><a class="headerlink" href="#django.contrib.admin.InlineModelAdmin" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<dl class="class">
<dt id="django.contrib.admin.TabularInline">
<em class="property">class </em><code class="descname">TabularInline</code><a class="reference internal" href="../../../_modules/django/contrib/admin/options.html#TabularInline"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.contrib.admin.TabularInline" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<dl class="class">
<dt id="django.contrib.admin.StackedInline">
<em class="property">class </em><code class="descname">StackedInline</code><a class="reference internal" href="../../../_modules/django/contrib/admin/options.html#StackedInline"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.contrib.admin.StackedInline" title="Permalink to this definition">¶</a></dt>
<dd><p>The admin interface has the ability to edit models on the same page as a
parent model. These are called inlines. Suppose you have these two models:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.db</span> <span class="k">import</span> <span class="n">models</span>

<span class="k">class</span> <span class="nc">Author</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
   <span class="n">name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>

<span class="k">class</span> <span class="nc">Book</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
   <span class="n">author</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">Author</span><span class="p">,</span> <span class="n">on_delete</span><span class="o">=</span><span class="n">models</span><span class="o">.</span><span class="n">CASCADE</span><span class="p">)</span>
   <span class="n">title</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>
</pre></div>
</div>
<p>You can edit the books authored by an author on the author page. You add
inlines to a model by specifying them in a <code class="docutils literal notranslate"><span class="pre">ModelAdmin.inlines</span></code>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib</span> <span class="k">import</span> <span class="n">admin</span>

<span class="k">class</span> <span class="nc">BookInline</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">TabularInline</span><span class="p">):</span>
    <span class="n">model</span> <span class="o">=</span> <span class="n">Book</span>

<span class="k">class</span> <span class="nc">AuthorAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">inlines</span> <span class="o">=</span> <span class="p">[</span>
        <span class="n">BookInline</span><span class="p">,</span>
    <span class="p">]</span>
</pre></div>
</div>
<p>Django provides two subclasses of <code class="docutils literal notranslate"><span class="pre">InlineModelAdmin</span></code> and they are:</p>
<ul class="simple">
<li><a class="reference internal" href="#django.contrib.admin.TabularInline" title="django.contrib.admin.TabularInline"><code class="xref py py-class docutils literal notranslate"><span class="pre">TabularInline</span></code></a></li>
<li><a class="reference internal" href="#django.contrib.admin.StackedInline" title="django.contrib.admin.StackedInline"><code class="xref py py-class docutils literal notranslate"><span class="pre">StackedInline</span></code></a></li>
</ul>
<p>The difference between these two is merely the template used to render
them.</p>
</dd></dl>

<div class="section" id="s-inlinemodeladmin-options">
<span id="inlinemodeladmin-options"></span><h3><code class="docutils literal notranslate"><span class="pre">InlineModelAdmin</span></code> options<a class="headerlink" href="#inlinemodeladmin-options" title="Permalink to this headline">¶</a></h3>
<p><code class="docutils literal notranslate"><span class="pre">InlineModelAdmin</span></code> shares many of the same features as <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code>, and
adds some of its own (the shared features are actually defined in the
<code class="docutils literal notranslate"><span class="pre">BaseModelAdmin</span></code> superclass). The shared features are:</p>
<ul class="simple">
<li><a class="reference internal" href="#django.contrib.admin.InlineModelAdmin.form" title="django.contrib.admin.InlineModelAdmin.form"><code class="xref py py-attr docutils literal notranslate"><span class="pre">form</span></code></a></li>
<li><a class="reference internal" href="#django.contrib.admin.ModelAdmin.fieldsets" title="django.contrib.admin.ModelAdmin.fieldsets"><code class="xref py py-attr docutils literal notranslate"><span class="pre">fieldsets</span></code></a></li>
<li><a class="reference internal" href="#django.contrib.admin.ModelAdmin.fields" title="django.contrib.admin.ModelAdmin.fields"><code class="xref py py-attr docutils literal notranslate"><span class="pre">fields</span></code></a></li>
<li><a class="reference internal" href="#django.contrib.admin.ModelAdmin.formfield_overrides" title="django.contrib.admin.ModelAdmin.formfield_overrides"><code class="xref py py-attr docutils literal notranslate"><span class="pre">formfield_overrides</span></code></a></li>
<li><a class="reference internal" href="#django.contrib.admin.ModelAdmin.exclude" title="django.contrib.admin.ModelAdmin.exclude"><code class="xref py py-attr docutils literal notranslate"><span class="pre">exclude</span></code></a></li>
<li><a class="reference internal" href="#django.contrib.admin.ModelAdmin.filter_horizontal" title="django.contrib.admin.ModelAdmin.filter_horizontal"><code class="xref py py-attr docutils literal notranslate"><span class="pre">filter_horizontal</span></code></a></li>
<li><a class="reference internal" href="#django.contrib.admin.ModelAdmin.filter_vertical" title="django.contrib.admin.ModelAdmin.filter_vertical"><code class="xref py py-attr docutils literal notranslate"><span class="pre">filter_vertical</span></code></a></li>
<li><a class="reference internal" href="#django.contrib.admin.ModelAdmin.ordering" title="django.contrib.admin.ModelAdmin.ordering"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ordering</span></code></a></li>
<li><a class="reference internal" href="#django.contrib.admin.ModelAdmin.prepopulated_fields" title="django.contrib.admin.ModelAdmin.prepopulated_fields"><code class="xref py py-attr docutils literal notranslate"><span class="pre">prepopulated_fields</span></code></a></li>
<li><a class="reference internal" href="#django.contrib.admin.ModelAdmin.get_queryset" title="django.contrib.admin.ModelAdmin.get_queryset"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_queryset()</span></code></a></li>
<li><a class="reference internal" href="#django.contrib.admin.ModelAdmin.radio_fields" title="django.contrib.admin.ModelAdmin.radio_fields"><code class="xref py py-attr docutils literal notranslate"><span class="pre">radio_fields</span></code></a></li>
<li><a class="reference internal" href="#django.contrib.admin.ModelAdmin.readonly_fields" title="django.contrib.admin.ModelAdmin.readonly_fields"><code class="xref py py-attr docutils literal notranslate"><span class="pre">readonly_fields</span></code></a></li>
<li><a class="reference internal" href="#django.contrib.admin.InlineModelAdmin.raw_id_fields" title="django.contrib.admin.InlineModelAdmin.raw_id_fields"><code class="xref py py-attr docutils literal notranslate"><span class="pre">raw_id_fields</span></code></a></li>
<li><a class="reference internal" href="#django.contrib.admin.ModelAdmin.formfield_for_choice_field" title="django.contrib.admin.ModelAdmin.formfield_for_choice_field"><code class="xref py py-meth docutils literal notranslate"><span class="pre">formfield_for_choice_field()</span></code></a></li>
<li><a class="reference internal" href="#django.contrib.admin.ModelAdmin.formfield_for_foreignkey" title="django.contrib.admin.ModelAdmin.formfield_for_foreignkey"><code class="xref py py-meth docutils literal notranslate"><span class="pre">formfield_for_foreignkey()</span></code></a></li>
<li><a class="reference internal" href="#django.contrib.admin.ModelAdmin.formfield_for_manytomany" title="django.contrib.admin.ModelAdmin.formfield_for_manytomany"><code class="xref py py-meth docutils literal notranslate"><span class="pre">formfield_for_manytomany()</span></code></a></li>
<li><a class="reference internal" href="#django.contrib.admin.ModelAdmin.has_add_permission" title="django.contrib.admin.ModelAdmin.has_add_permission"><code class="xref py py-meth docutils literal notranslate"><span class="pre">has_add_permission()</span></code></a></li>
<li><a class="reference internal" href="#django.contrib.admin.ModelAdmin.has_change_permission" title="django.contrib.admin.ModelAdmin.has_change_permission"><code class="xref py py-meth docutils literal notranslate"><span class="pre">has_change_permission()</span></code></a></li>
<li><a class="reference internal" href="#django.contrib.admin.ModelAdmin.has_delete_permission" title="django.contrib.admin.ModelAdmin.has_delete_permission"><code class="xref py py-meth docutils literal notranslate"><span class="pre">has_delete_permission()</span></code></a></li>
<li><a class="reference internal" href="#django.contrib.admin.ModelAdmin.has_module_permission" title="django.contrib.admin.ModelAdmin.has_module_permission"><code class="xref py py-meth docutils literal notranslate"><span class="pre">has_module_permission()</span></code></a></li>
</ul>
<p>The <code class="docutils literal notranslate"><span class="pre">InlineModelAdmin</span></code> class adds:</p>
<dl class="attribute">
<dt id="django.contrib.admin.InlineModelAdmin.model">
<code class="descclassname">InlineModelAdmin.</code><code class="descname">model</code><a class="headerlink" href="#django.contrib.admin.InlineModelAdmin.model" title="Permalink to this definition">¶</a></dt>
<dd><p>The model which the inline is using. This is required.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.InlineModelAdmin.fk_name">
<code class="descclassname">InlineModelAdmin.</code><code class="descname">fk_name</code><a class="headerlink" href="#django.contrib.admin.InlineModelAdmin.fk_name" title="Permalink to this definition">¶</a></dt>
<dd><p>The name of the foreign key on the model. In most cases this will be dealt
with automatically, but <code class="docutils literal notranslate"><span class="pre">fk_name</span></code> must be specified explicitly if there
are more than one foreign key to the same parent model.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.InlineModelAdmin.formset">
<code class="descclassname">InlineModelAdmin.</code><code class="descname">formset</code><a class="headerlink" href="#django.contrib.admin.InlineModelAdmin.formset" title="Permalink to this definition">¶</a></dt>
<dd><p>This defaults to <a class="reference internal" href="../../../topics/forms/modelforms.html#django.forms.models.BaseInlineFormSet" title="django.forms.models.BaseInlineFormSet"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseInlineFormSet</span></code></a>. Using
your own formset can give you many possibilities of customization. Inlines
are built around <a class="reference internal" href="../../../topics/forms/modelforms.html#model-formsets"><span class="std std-ref">model formsets</span></a>.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.InlineModelAdmin.form">
<code class="descclassname">InlineModelAdmin.</code><code class="descname">form</code><a class="headerlink" href="#django.contrib.admin.InlineModelAdmin.form" title="Permalink to this definition">¶</a></dt>
<dd><p>The value for <code class="docutils literal notranslate"><span class="pre">form</span></code> defaults to <code class="docutils literal notranslate"><span class="pre">ModelForm</span></code>. This is what is passed
through to <a class="reference internal" href="../../forms/models.html#django.forms.models.inlineformset_factory" title="django.forms.models.inlineformset_factory"><code class="xref py py-func docutils literal notranslate"><span class="pre">inlineformset_factory()</span></code></a> when
creating the formset for this inline.</p>
</dd></dl>

<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">When writing custom validation for <code class="docutils literal notranslate"><span class="pre">InlineModelAdmin</span></code> forms, be cautious
of writing validation that relies on features of the parent model. If the
parent model fails to validate, it may be left in an inconsistent state as
described in the warning in <a class="reference internal" href="../../../topics/forms/modelforms.html#validation-on-modelform"><span class="std std-ref">Validation on a ModelForm</span></a>.</p>
</div>
<dl class="attribute">
<dt id="django.contrib.admin.InlineModelAdmin.classes">
<code class="descclassname">InlineModelAdmin.</code><code class="descname">classes</code><a class="headerlink" href="#django.contrib.admin.InlineModelAdmin.classes" title="Permalink to this definition">¶</a></dt>
<dd><div class="versionadded">
<span class="title">New in Django 1.10.</span> </div>
<p>A list or tuple containing extra CSS classes to apply to the fieldset that
is rendered for the inlines. Defaults to <code class="docutils literal notranslate"><span class="pre">None</span></code>. As with classes
configured in <a class="reference internal" href="#django.contrib.admin.ModelAdmin.fieldsets" title="django.contrib.admin.ModelAdmin.fieldsets"><code class="xref py py-attr docutils literal notranslate"><span class="pre">fieldsets</span></code></a>, inlines with a <code class="docutils literal notranslate"><span class="pre">collapse</span></code>
class will be initially collapsed and their header will have a small “show”
link.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.InlineModelAdmin.extra">
<code class="descclassname">InlineModelAdmin.</code><code class="descname">extra</code><a class="headerlink" href="#django.contrib.admin.InlineModelAdmin.extra" title="Permalink to this definition">¶</a></dt>
<dd><p>This controls the number of extra forms the formset will display in
addition to the initial forms. See the
<a class="reference internal" href="../../../topics/forms/formsets.html"><span class="doc">formsets documentation</span></a> for more
information.</p>
<p>For users with JavaScript-enabled browsers, an “Add another” link is
provided to enable any number of additional inlines to be added in addition
to those provided as a result of the <code class="docutils literal notranslate"><span class="pre">extra</span></code> argument.</p>
<p>The dynamic link will not appear if the number of currently displayed forms
exceeds <code class="docutils literal notranslate"><span class="pre">max_num</span></code>, or if the user does not have JavaScript enabled.</p>
<p><a class="reference internal" href="#django.contrib.admin.InlineModelAdmin.get_extra" title="django.contrib.admin.InlineModelAdmin.get_extra"><code class="xref py py-meth docutils literal notranslate"><span class="pre">InlineModelAdmin.get_extra()</span></code></a> also allows you to customize the number
of extra forms.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.InlineModelAdmin.max_num">
<code class="descclassname">InlineModelAdmin.</code><code class="descname">max_num</code><a class="headerlink" href="#django.contrib.admin.InlineModelAdmin.max_num" title="Permalink to this definition">¶</a></dt>
<dd><p>This controls the maximum number of forms to show in the inline. This
doesn’t directly correlate to the number of objects, but can if the value
is small enough. See <a class="reference internal" href="../../../topics/forms/modelforms.html#model-formsets-max-num"><span class="std std-ref">Limiting the number of editable objects</span></a> for more information.</p>
<p><a class="reference internal" href="#django.contrib.admin.InlineModelAdmin.get_max_num" title="django.contrib.admin.InlineModelAdmin.get_max_num"><code class="xref py py-meth docutils literal notranslate"><span class="pre">InlineModelAdmin.get_max_num()</span></code></a> also allows you to customize the
maximum number of extra forms.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.InlineModelAdmin.min_num">
<code class="descclassname">InlineModelAdmin.</code><code class="descname">min_num</code><a class="headerlink" href="#django.contrib.admin.InlineModelAdmin.min_num" title="Permalink to this definition">¶</a></dt>
<dd><p>This controls the minimum number of forms to show in the inline.
See <a class="reference internal" href="../../forms/models.html#django.forms.models.modelformset_factory" title="django.forms.models.modelformset_factory"><code class="xref py py-func docutils literal notranslate"><span class="pre">modelformset_factory()</span></code></a> for more information.</p>
<p><a class="reference internal" href="#django.contrib.admin.InlineModelAdmin.get_min_num" title="django.contrib.admin.InlineModelAdmin.get_min_num"><code class="xref py py-meth docutils literal notranslate"><span class="pre">InlineModelAdmin.get_min_num()</span></code></a> also allows you to customize the
minimum number of displayed forms.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.InlineModelAdmin.raw_id_fields">
<code class="descclassname">InlineModelAdmin.</code><code class="descname">raw_id_fields</code><a class="headerlink" href="#django.contrib.admin.InlineModelAdmin.raw_id_fields" title="Permalink to this definition">¶</a></dt>
<dd><p>By default, Django’s admin uses a select-box interface (&lt;select&gt;) for
fields that are <code class="docutils literal notranslate"><span class="pre">ForeignKey</span></code>. Sometimes you don’t want to incur the
overhead of having to select all the related instances to display in the
drop-down.</p>
<p><code class="docutils literal notranslate"><span class="pre">raw_id_fields</span></code> is a list of fields you would like to change into an
<code class="docutils literal notranslate"><span class="pre">Input</span></code> widget for either a <code class="docutils literal notranslate"><span class="pre">ForeignKey</span></code> or <code class="docutils literal notranslate"><span class="pre">ManyToManyField</span></code>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">BookInline</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">TabularInline</span><span class="p">):</span>
    <span class="n">model</span> <span class="o">=</span> <span class="n">Book</span>
    <span class="n">raw_id_fields</span> <span class="o">=</span> <span class="p">(</span><span class="s2">&quot;pages&quot;</span><span class="p">,)</span>
</pre></div>
</div>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.InlineModelAdmin.template">
<code class="descclassname">InlineModelAdmin.</code><code class="descname">template</code><a class="headerlink" href="#django.contrib.admin.InlineModelAdmin.template" title="Permalink to this definition">¶</a></dt>
<dd><p>The template used to render the inline on the page.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.InlineModelAdmin.verbose_name">
<code class="descclassname">InlineModelAdmin.</code><code class="descname">verbose_name</code><a class="headerlink" href="#django.contrib.admin.InlineModelAdmin.verbose_name" title="Permalink to this definition">¶</a></dt>
<dd><p>An override to the <code class="docutils literal notranslate"><span class="pre">verbose_name</span></code> found in the model’s inner <code class="docutils literal notranslate"><span class="pre">Meta</span></code>
class.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.InlineModelAdmin.verbose_name_plural">
<code class="descclassname">InlineModelAdmin.</code><code class="descname">verbose_name_plural</code><a class="headerlink" href="#django.contrib.admin.InlineModelAdmin.verbose_name_plural" title="Permalink to this definition">¶</a></dt>
<dd><p>An override to the <code class="docutils literal notranslate"><span class="pre">verbose_name_plural</span></code> found in the model’s inner
<code class="docutils literal notranslate"><span class="pre">Meta</span></code> class.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.InlineModelAdmin.can_delete">
<code class="descclassname">InlineModelAdmin.</code><code class="descname">can_delete</code><a class="headerlink" href="#django.contrib.admin.InlineModelAdmin.can_delete" title="Permalink to this definition">¶</a></dt>
<dd><p>Specifies whether or not inline objects can be deleted in the inline.
Defaults to <code class="docutils literal notranslate"><span class="pre">True</span></code>.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.InlineModelAdmin.show_change_link">
<code class="descclassname">InlineModelAdmin.</code><code class="descname">show_change_link</code><a class="headerlink" href="#django.contrib.admin.InlineModelAdmin.show_change_link" title="Permalink to this definition">¶</a></dt>
<dd><p>Specifies whether or not inline objects that can be changed in the
admin have a link to the change form. Defaults to <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.InlineModelAdmin.get_formset">
<code class="descclassname">InlineModelAdmin.</code><code class="descname">get_formset</code>(<em>request</em>, <em>obj=None</em>, <em>**kwargs</em>)<a class="headerlink" href="#django.contrib.admin.InlineModelAdmin.get_formset" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a <a class="reference internal" href="../../../topics/forms/modelforms.html#django.forms.models.BaseInlineFormSet" title="django.forms.models.BaseInlineFormSet"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseInlineFormSet</span></code></a> class for use in
admin add/change views. See the example for
<a class="reference internal" href="#django.contrib.admin.ModelAdmin.get_formsets_with_inlines" title="django.contrib.admin.ModelAdmin.get_formsets_with_inlines"><code class="xref py py-class docutils literal notranslate"><span class="pre">ModelAdmin.get_formsets_with_inlines</span></code></a>.</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.InlineModelAdmin.get_extra">
<code class="descclassname">InlineModelAdmin.</code><code class="descname">get_extra</code>(<em>request</em>, <em>obj=None</em>, <em>**kwargs</em>)<a class="headerlink" href="#django.contrib.admin.InlineModelAdmin.get_extra" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the number of extra inline forms to use. By default, returns the
<a class="reference internal" href="#django.contrib.admin.InlineModelAdmin.extra" title="django.contrib.admin.InlineModelAdmin.extra"><code class="xref py py-attr docutils literal notranslate"><span class="pre">InlineModelAdmin.extra</span></code></a> attribute.</p>
<p>Override this method to programmatically determine the number of extra
inline forms. For example, this may be based on the model instance
(passed as the keyword argument <code class="docutils literal notranslate"><span class="pre">obj</span></code>):</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">BinaryTreeAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">TabularInline</span><span class="p">):</span>
    <span class="n">model</span> <span class="o">=</span> <span class="n">BinaryTree</span>

    <span class="k">def</span> <span class="nf">get_extra</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="n">extra</span> <span class="o">=</span> <span class="mi">2</span>
        <span class="k">if</span> <span class="n">obj</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">extra</span> <span class="o">-</span> <span class="n">obj</span><span class="o">.</span><span class="n">binarytree_set</span><span class="o">.</span><span class="n">count</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">extra</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.InlineModelAdmin.get_max_num">
<code class="descclassname">InlineModelAdmin.</code><code class="descname">get_max_num</code>(<em>request</em>, <em>obj=None</em>, <em>**kwargs</em>)<a class="headerlink" href="#django.contrib.admin.InlineModelAdmin.get_max_num" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the maximum number of extra inline forms to use. By default,
returns the <a class="reference internal" href="#django.contrib.admin.InlineModelAdmin.max_num" title="django.contrib.admin.InlineModelAdmin.max_num"><code class="xref py py-attr docutils literal notranslate"><span class="pre">InlineModelAdmin.max_num</span></code></a> attribute.</p>
<p>Override this method to programmatically determine the maximum number of
inline forms. For example, this may be based on the model instance
(passed as the keyword argument <code class="docutils literal notranslate"><span class="pre">obj</span></code>):</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">BinaryTreeAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">TabularInline</span><span class="p">):</span>
    <span class="n">model</span> <span class="o">=</span> <span class="n">BinaryTree</span>

    <span class="k">def</span> <span class="nf">get_max_num</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="n">max_num</span> <span class="o">=</span> <span class="mi">10</span>
        <span class="k">if</span> <span class="n">obj</span> <span class="ow">and</span> <span class="n">obj</span><span class="o">.</span><span class="n">parent</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">max_num</span> <span class="o">-</span> <span class="mi">5</span>
        <span class="k">return</span> <span class="n">max_num</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.InlineModelAdmin.get_min_num">
<code class="descclassname">InlineModelAdmin.</code><code class="descname">get_min_num</code>(<em>request</em>, <em>obj=None</em>, <em>**kwargs</em>)<a class="headerlink" href="#django.contrib.admin.InlineModelAdmin.get_min_num" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the minimum number of inline forms to use. By default,
returns the <a class="reference internal" href="#django.contrib.admin.InlineModelAdmin.min_num" title="django.contrib.admin.InlineModelAdmin.min_num"><code class="xref py py-attr docutils literal notranslate"><span class="pre">InlineModelAdmin.min_num</span></code></a> attribute.</p>
<p>Override this method to programmatically determine the minimum number of
inline forms. For example, this may be based on the model instance
(passed as the keyword argument <code class="docutils literal notranslate"><span class="pre">obj</span></code>).</p>
</dd></dl>

</div>
<div class="section" id="s-working-with-a-model-with-two-or-more-foreign-keys-to-the-same-parent-model">
<span id="working-with-a-model-with-two-or-more-foreign-keys-to-the-same-parent-model"></span><h3>Working with a model with two or more foreign keys to the same parent model<a class="headerlink" href="#working-with-a-model-with-two-or-more-foreign-keys-to-the-same-parent-model" title="Permalink to this headline">¶</a></h3>
<p>It is sometimes possible to have more than one foreign key to the same model.
Take this model for instance:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.db</span> <span class="k">import</span> <span class="n">models</span>

<span class="k">class</span> <span class="nc">Friendship</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">to_person</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">Person</span><span class="p">,</span> <span class="n">on_delete</span><span class="o">=</span><span class="n">models</span><span class="o">.</span><span class="n">CASCADE</span><span class="p">,</span> <span class="n">related_name</span><span class="o">=</span><span class="s2">&quot;friends&quot;</span><span class="p">)</span>
    <span class="n">from_person</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">Person</span><span class="p">,</span> <span class="n">on_delete</span><span class="o">=</span><span class="n">models</span><span class="o">.</span><span class="n">CASCADE</span><span class="p">,</span> <span class="n">related_name</span><span class="o">=</span><span class="s2">&quot;from_friends&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>If you wanted to display an inline on the <code class="docutils literal notranslate"><span class="pre">Person</span></code> admin add/change pages
you need to explicitly define the foreign key since it is unable to do so
automatically:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib</span> <span class="k">import</span> <span class="n">admin</span>
<span class="kn">from</span> <span class="nn">myapp.models</span> <span class="k">import</span> <span class="n">Friendship</span>

<span class="k">class</span> <span class="nc">FriendshipInline</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">TabularInline</span><span class="p">):</span>
    <span class="n">model</span> <span class="o">=</span> <span class="n">Friendship</span>
    <span class="n">fk_name</span> <span class="o">=</span> <span class="s2">&quot;to_person&quot;</span>

<span class="k">class</span> <span class="nc">PersonAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">inlines</span> <span class="o">=</span> <span class="p">[</span>
        <span class="n">FriendshipInline</span><span class="p">,</span>
    <span class="p">]</span>
</pre></div>
</div>
</div>
<div class="section" id="s-working-with-many-to-many-models">
<span id="working-with-many-to-many-models"></span><h3>Working with many-to-many models<a class="headerlink" href="#working-with-many-to-many-models" title="Permalink to this headline">¶</a></h3>
<p>By default, admin widgets for many-to-many relations will be displayed
on whichever model contains the actual reference to the
<a class="reference internal" href="../../models/fields.html#django.db.models.ManyToManyField" title="django.db.models.ManyToManyField"><code class="xref py py-class docutils literal notranslate"><span class="pre">ManyToManyField</span></code></a>. Depending on your <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code>
definition, each many-to-many field in your model will be represented by a
standard HTML <code class="docutils literal notranslate"><span class="pre">&lt;select</span> <span class="pre">multiple&gt;</span></code>, a horizontal or vertical filter, or a
<code class="docutils literal notranslate"><span class="pre">raw_id_admin</span></code> widget. However, it is also possible to replace these
widgets with inlines.</p>
<p>Suppose we have the following models:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.db</span> <span class="k">import</span> <span class="n">models</span>

<span class="k">class</span> <span class="nc">Person</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">128</span><span class="p">)</span>

<span class="k">class</span> <span class="nc">Group</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">128</span><span class="p">)</span>
    <span class="n">members</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ManyToManyField</span><span class="p">(</span><span class="n">Person</span><span class="p">,</span> <span class="n">related_name</span><span class="o">=</span><span class="s1">&#39;groups&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>If you want to display many-to-many relations using an inline, you can do
so by defining an <code class="docutils literal notranslate"><span class="pre">InlineModelAdmin</span></code> object for the relationship:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib</span> <span class="k">import</span> <span class="n">admin</span>

<span class="k">class</span> <span class="nc">MembershipInline</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">TabularInline</span><span class="p">):</span>
    <span class="n">model</span> <span class="o">=</span> <span class="n">Group</span><span class="o">.</span><span class="n">members</span><span class="o">.</span><span class="n">through</span>

<span class="k">class</span> <span class="nc">PersonAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">inlines</span> <span class="o">=</span> <span class="p">[</span>
        <span class="n">MembershipInline</span><span class="p">,</span>
    <span class="p">]</span>

<span class="k">class</span> <span class="nc">GroupAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">inlines</span> <span class="o">=</span> <span class="p">[</span>
        <span class="n">MembershipInline</span><span class="p">,</span>
    <span class="p">]</span>
    <span class="n">exclude</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;members&#39;</span><span class="p">,)</span>
</pre></div>
</div>
<p>There are two features worth noting in this example.</p>
<p>Firstly - the <code class="docutils literal notranslate"><span class="pre">MembershipInline</span></code> class references <code class="docutils literal notranslate"><span class="pre">Group.members.through</span></code>.
The <code class="docutils literal notranslate"><span class="pre">through</span></code> attribute is a reference to the model that manages the
many-to-many relation. This model is automatically created by Django when you
define a many-to-many field.</p>
<p>Secondly, the <code class="docutils literal notranslate"><span class="pre">GroupAdmin</span></code> must manually exclude the <code class="docutils literal notranslate"><span class="pre">members</span></code> field.
Django displays an admin widget for a many-to-many field on the model that
defines the relation (in this case, <code class="docutils literal notranslate"><span class="pre">Group</span></code>). If you want to use an inline
model to represent the many-to-many relationship, you must tell Django’s admin
to <em>not</em> display this widget - otherwise you will end up with two widgets on
your admin page for managing the relation.</p>
<p>Note that when using this technique the
<a class="reference internal" href="../../signals.html#django.db.models.signals.m2m_changed" title="django.db.models.signals.m2m_changed"><code class="xref py py-data docutils literal notranslate"><span class="pre">m2m_changed</span></code></a> signals aren’t triggered. This
is because as far as the admin is concerned, <code class="docutils literal notranslate"><span class="pre">through</span></code> is just a model with
two foreign key fields rather than a many-to-many relation.</p>
<p>In all other respects, the <code class="docutils literal notranslate"><span class="pre">InlineModelAdmin</span></code> is exactly the same as any
other. You can customize the appearance using any of the normal
<code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> properties.</p>
</div>
<div class="section" id="s-working-with-many-to-many-intermediary-models">
<span id="working-with-many-to-many-intermediary-models"></span><h3>Working with many-to-many intermediary models<a class="headerlink" href="#working-with-many-to-many-intermediary-models" title="Permalink to this headline">¶</a></h3>
<p>When you specify an intermediary model using the <code class="docutils literal notranslate"><span class="pre">through</span></code> argument to a
<a class="reference internal" href="../../models/fields.html#django.db.models.ManyToManyField" title="django.db.models.ManyToManyField"><code class="xref py py-class docutils literal notranslate"><span class="pre">ManyToManyField</span></code></a>, the admin will not display a
widget by default. This is because each instance of that intermediary model
requires more information than could be displayed in a single widget, and the
layout required for multiple widgets will vary depending on the intermediate
model.</p>
<p>However, we still want to be able to edit that information inline. Fortunately,
this is easy to do with inline admin models. Suppose we have the following
models:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.db</span> <span class="k">import</span> <span class="n">models</span>

<span class="k">class</span> <span class="nc">Person</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">128</span><span class="p">)</span>

<span class="k">class</span> <span class="nc">Group</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">128</span><span class="p">)</span>
    <span class="n">members</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ManyToManyField</span><span class="p">(</span><span class="n">Person</span><span class="p">,</span> <span class="n">through</span><span class="o">=</span><span class="s1">&#39;Membership&#39;</span><span class="p">)</span>

<span class="k">class</span> <span class="nc">Membership</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">person</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">Person</span><span class="p">,</span> <span class="n">on_delete</span><span class="o">=</span><span class="n">models</span><span class="o">.</span><span class="n">CASCADE</span><span class="p">)</span>
    <span class="n">group</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">Group</span><span class="p">,</span> <span class="n">on_delete</span><span class="o">=</span><span class="n">models</span><span class="o">.</span><span class="n">CASCADE</span><span class="p">)</span>
    <span class="n">date_joined</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">DateField</span><span class="p">()</span>
    <span class="n">invite_reason</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">64</span><span class="p">)</span>
</pre></div>
</div>
<p>The first step in displaying this intermediate model in the admin is to
define an inline class for the <code class="docutils literal notranslate"><span class="pre">Membership</span></code> model:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">MembershipInline</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">TabularInline</span><span class="p">):</span>
    <span class="n">model</span> <span class="o">=</span> <span class="n">Membership</span>
    <span class="n">extra</span> <span class="o">=</span> <span class="mi">1</span>
</pre></div>
</div>
<p>This simple example uses the default <code class="docutils literal notranslate"><span class="pre">InlineModelAdmin</span></code> values for the
<code class="docutils literal notranslate"><span class="pre">Membership</span></code> model, and limits the extra add forms to one. This could be
customized using any of the options available to <code class="docutils literal notranslate"><span class="pre">InlineModelAdmin</span></code> classes.</p>
<p>Now create admin views for the <code class="docutils literal notranslate"><span class="pre">Person</span></code> and <code class="docutils literal notranslate"><span class="pre">Group</span></code> models:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">PersonAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">inlines</span> <span class="o">=</span> <span class="p">(</span><span class="n">MembershipInline</span><span class="p">,)</span>

<span class="k">class</span> <span class="nc">GroupAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">inlines</span> <span class="o">=</span> <span class="p">(</span><span class="n">MembershipInline</span><span class="p">,)</span>
</pre></div>
</div>
<p>Finally, register your <code class="docutils literal notranslate"><span class="pre">Person</span></code> and <code class="docutils literal notranslate"><span class="pre">Group</span></code> models with the admin site:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">admin</span><span class="o">.</span><span class="n">site</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">Person</span><span class="p">,</span> <span class="n">PersonAdmin</span><span class="p">)</span>
<span class="n">admin</span><span class="o">.</span><span class="n">site</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">Group</span><span class="p">,</span> <span class="n">GroupAdmin</span><span class="p">)</span>
</pre></div>
</div>
<p>Now your admin site is set up to edit <code class="docutils literal notranslate"><span class="pre">Membership</span></code> objects inline from
either the <code class="docutils literal notranslate"><span class="pre">Person</span></code> or the <code class="docutils literal notranslate"><span class="pre">Group</span></code> detail pages.</p>
</div>
<div class="section" id="s-using-generic-relations-as-an-inline">
<span id="s-id3"></span><span id="using-generic-relations-as-an-inline"></span><span id="id3"></span><h3>Using generic relations as an inline<a class="headerlink" href="#using-generic-relations-as-an-inline" title="Permalink to this headline">¶</a></h3>
<p>It is possible to use an inline with generically related objects. Let’s say
you have the following models:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.db</span> <span class="k">import</span> <span class="n">models</span>
<span class="kn">from</span> <span class="nn">django.contrib.contenttypes.fields</span> <span class="k">import</span> <span class="n">GenericForeignKey</span>

<span class="k">class</span> <span class="nc">Image</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">image</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ImageField</span><span class="p">(</span><span class="n">upload_to</span><span class="o">=</span><span class="s2">&quot;images&quot;</span><span class="p">)</span>
    <span class="n">content_type</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">ContentType</span><span class="p">,</span> <span class="n">on_delete</span><span class="o">=</span><span class="n">models</span><span class="o">.</span><span class="n">CASCADE</span><span class="p">)</span>
    <span class="n">object_id</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">PositiveIntegerField</span><span class="p">()</span>
    <span class="n">content_object</span> <span class="o">=</span> <span class="n">GenericForeignKey</span><span class="p">(</span><span class="s2">&quot;content_type&quot;</span><span class="p">,</span> <span class="s2">&quot;object_id&quot;</span><span class="p">)</span>

<span class="k">class</span> <span class="nc">Product</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>
</pre></div>
</div>
<p>If you want to allow editing and creating an <code class="docutils literal notranslate"><span class="pre">Image</span></code> instance on the
<code class="docutils literal notranslate"><span class="pre">Product</span></code>, add/change views you can use
<a class="reference internal" href="../contenttypes.html#django.contrib.contenttypes.admin.GenericTabularInline" title="django.contrib.contenttypes.admin.GenericTabularInline"><code class="xref py py-class docutils literal notranslate"><span class="pre">GenericTabularInline</span></code></a>
or <a class="reference internal" href="../contenttypes.html#django.contrib.contenttypes.admin.GenericStackedInline" title="django.contrib.contenttypes.admin.GenericStackedInline"><code class="xref py py-class docutils literal notranslate"><span class="pre">GenericStackedInline</span></code></a> (both
subclasses of <a class="reference internal" href="../contenttypes.html#django.contrib.contenttypes.admin.GenericInlineModelAdmin" title="django.contrib.contenttypes.admin.GenericInlineModelAdmin"><code class="xref py py-class docutils literal notranslate"><span class="pre">GenericInlineModelAdmin</span></code></a>)
provided by <a class="reference internal" href="../contenttypes.html#module-django.contrib.contenttypes.admin" title="django.contrib.contenttypes.admin"><code class="xref py py-mod docutils literal notranslate"><span class="pre">admin</span></code></a>. They implement tabular
and stacked visual layouts for the forms representing the inline objects,
respectively, just like their non-generic counterparts. They behave just like
any other inline. In your <code class="docutils literal notranslate"><span class="pre">admin.py</span></code> for this example app:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib</span> <span class="k">import</span> <span class="n">admin</span>
<span class="kn">from</span> <span class="nn">django.contrib.contenttypes.admin</span> <span class="k">import</span> <span class="n">GenericTabularInline</span>

<span class="kn">from</span> <span class="nn">myproject.myapp.models</span> <span class="k">import</span> <span class="n">Image</span><span class="p">,</span> <span class="n">Product</span>

<span class="k">class</span> <span class="nc">ImageInline</span><span class="p">(</span><span class="n">GenericTabularInline</span><span class="p">):</span>
    <span class="n">model</span> <span class="o">=</span> <span class="n">Image</span>

<span class="k">class</span> <span class="nc">ProductAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">inlines</span> <span class="o">=</span> <span class="p">[</span>
        <span class="n">ImageInline</span><span class="p">,</span>
    <span class="p">]</span>

<span class="n">admin</span><span class="o">.</span><span class="n">site</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">Product</span><span class="p">,</span> <span class="n">ProductAdmin</span><span class="p">)</span>
</pre></div>
</div>
<p>See the <a class="reference internal" href="../contenttypes.html"><span class="doc">contenttypes documentation</span></a> for more
specific information.</p>
</div>
</div>
<div class="section" id="s-overriding-admin-templates">
<span id="s-admin-overriding-templates"></span><span id="overriding-admin-templates"></span><span id="admin-overriding-templates"></span><h2>Overriding admin templates<a class="headerlink" href="#overriding-admin-templates" title="Permalink to this headline">¶</a></h2>
<p>It is relatively easy to override many of the templates which the admin module
uses to generate the various pages of an admin site. You can even override a
few of these templates for a specific app, or a specific model.</p>
<div class="section" id="s-set-up-your-projects-admin-template-directories">
<span id="set-up-your-projects-admin-template-directories"></span><h3>Set up your projects admin template directories<a class="headerlink" href="#set-up-your-projects-admin-template-directories" title="Permalink to this headline">¶</a></h3>
<p>The admin template files are located in the <code class="docutils literal notranslate"><span class="pre">contrib/admin/templates/admin</span></code>
directory.</p>
<p>In order to override one or more of them, first create an <code class="docutils literal notranslate"><span class="pre">admin</span></code> directory
in your project’s <code class="docutils literal notranslate"><span class="pre">templates</span></code> directory. This can be any of the directories
you specified in the <a class="reference internal" href="../../settings.html#std:setting-TEMPLATES-DIRS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">DIRS</span></code></a> option of the
<code class="docutils literal notranslate"><span class="pre">DjangoTemplates</span></code> backend in the <a class="reference internal" href="../../settings.html#std:setting-TEMPLATES"><code class="xref std std-setting docutils literal notranslate"><span class="pre">TEMPLATES</span></code></a> setting. If you have
customized the <code class="docutils literal notranslate"><span class="pre">'loaders'</span></code> option, be sure
<code class="docutils literal notranslate"><span class="pre">'django.template.loaders.filesystem.Loader'</span></code> appears before
<code class="docutils literal notranslate"><span class="pre">'django.template.loaders.app_directories.Loader'</span></code> so that your custom
templates will be found by the template loading system before those that are
included with <a class="reference internal" href="#module-django.contrib.admin" title="django.contrib.admin: Django's admin site."><code class="xref py py-mod docutils literal notranslate"><span class="pre">django.contrib.admin</span></code></a>.</p>
<p>Within this <code class="docutils literal notranslate"><span class="pre">admin</span></code> directory, create sub-directories named after your app.
Within these app subdirectories create sub-directories named after your models.
Note, that the admin app will lowercase the model name when looking for the
directory, so make sure you name the directory in all lowercase if you are
going to run your app on a case-sensitive filesystem.</p>
<p>To override an admin template for a specific app, copy and edit the template
from the <code class="docutils literal notranslate"><span class="pre">django/contrib/admin/templates/admin</span></code> directory, and save it to one
of the directories you just created.</p>
<p>For example, if we wanted to add a tool to the change list view for all the
models in an app named <code class="docutils literal notranslate"><span class="pre">my_app</span></code>, we would copy
<code class="docutils literal notranslate"><span class="pre">contrib/admin/templates/admin/change_list.html</span></code> to the
<code class="docutils literal notranslate"><span class="pre">templates/admin/my_app/</span></code> directory of our project, and make any necessary
changes.</p>
<p>If we wanted to add a tool to the change list view for only a specific model
named ‘Page’, we would copy that same file to the
<code class="docutils literal notranslate"><span class="pre">templates/admin/my_app/page</span></code> directory of our project.</p>
</div>
<div class="section" id="s-overriding-vs-replacing-an-admin-template">
<span id="overriding-vs-replacing-an-admin-template"></span><h3>Overriding vs. replacing an admin template<a class="headerlink" href="#overriding-vs-replacing-an-admin-template" title="Permalink to this headline">¶</a></h3>
<p>Because of the modular design of the admin templates, it is usually neither
necessary nor advisable to replace an entire template. It is almost always
better to override only the section of the template which you need to change.</p>
<p>To continue the example above, we want to add a new link next to the
<code class="docutils literal notranslate"><span class="pre">History</span></code> tool for the <code class="docutils literal notranslate"><span class="pre">Page</span></code> model. After looking at <code class="docutils literal notranslate"><span class="pre">change_form.html</span></code>
we determine that we only need to override the <code class="docutils literal notranslate"><span class="pre">object-tools-items</span></code> block.
Therefore here is our new <code class="docutils literal notranslate"><span class="pre">change_form.html</span></code> :</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">extends</span> <span class="s2">&quot;admin/change_form.html&quot;</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">load</span> <span class="nv">i18n</span> <span class="nv">admin_urls</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">block</span> <span class="nv">object-tools-items</span> <span class="cp">%}</span>
    <span class="p">&lt;</span><span class="nt">li</span><span class="p">&gt;</span>
        <span class="p">&lt;</span><span class="nt">a</span> <span class="na">href</span><span class="o">=</span><span class="s">&quot;</span><span class="cp">{%</span> <span class="k">url</span> <span class="nv">opts</span><span class="o">|</span><span class="nf">admin_urlname</span><span class="s1">:&#39;history&#39;</span> <span class="nv">original.pk</span><span class="o">|</span><span class="nf">admin_urlquote</span> <span class="cp">%}</span><span class="s">&quot;</span> <span class="na">class</span><span class="o">=</span><span class="s">&quot;historylink&quot;</span><span class="p">&gt;</span><span class="cp">{%</span> <span class="k">trans</span> <span class="s2">&quot;History&quot;</span> <span class="cp">%}</span><span class="p">&lt;/</span><span class="nt">a</span><span class="p">&gt;</span>
    <span class="p">&lt;/</span><span class="nt">li</span><span class="p">&gt;</span>
    <span class="p">&lt;</span><span class="nt">li</span><span class="p">&gt;</span>
        <span class="p">&lt;</span><span class="nt">a</span> <span class="na">href</span><span class="o">=</span><span class="s">&quot;mylink/&quot;</span> <span class="na">class</span><span class="o">=</span><span class="s">&quot;historylink&quot;</span><span class="p">&gt;</span>My Link<span class="p">&lt;/</span><span class="nt">a</span><span class="p">&gt;</span>
    <span class="p">&lt;/</span><span class="nt">li</span><span class="p">&gt;</span>
    <span class="cp">{%</span> <span class="k">if</span> <span class="nv">has_absolute_url</span> <span class="cp">%}</span>
        <span class="p">&lt;</span><span class="nt">li</span><span class="p">&gt;</span>
            <span class="p">&lt;</span><span class="nt">a</span> <span class="na">href</span><span class="o">=</span><span class="s">&quot;</span><span class="cp">{%</span> <span class="k">url</span> <span class="s1">&#39;admin:view_on_site&#39;</span> <span class="nv">content_type_id</span> <span class="nv">original.pk</span> <span class="cp">%}</span><span class="s">&quot;</span> <span class="na">class</span><span class="o">=</span><span class="s">&quot;viewsitelink&quot;</span><span class="p">&gt;</span><span class="cp">{%</span> <span class="k">trans</span> <span class="s2">&quot;View on site&quot;</span> <span class="cp">%}</span><span class="p">&lt;/</span><span class="nt">a</span><span class="p">&gt;</span>
        <span class="p">&lt;/</span><span class="nt">li</span><span class="p">&gt;</span>
    <span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">endblock</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>And that’s it! If we placed this file in the <code class="docutils literal notranslate"><span class="pre">templates/admin/my_app</span></code>
directory, our link would appear on the change form for all models within
my_app.</p>
</div>
<div class="section" id="s-templates-which-may-be-overridden-per-app-or-model">
<span id="templates-which-may-be-overridden-per-app-or-model"></span><h3>Templates which may be overridden per app or model<a class="headerlink" href="#templates-which-may-be-overridden-per-app-or-model" title="Permalink to this headline">¶</a></h3>
<p>Not every template in <code class="docutils literal notranslate"><span class="pre">contrib/admin/templates/admin</span></code> may be overridden per
app or per model. The following can:</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">app_index.html</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">change_form.html</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">change_list.html</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">delete_confirmation.html</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">object_history.html</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">popup_response.html</span></code></li>
</ul>
<div class="versionchanged">
<span class="title">Changed in Django 1.11:</span> <p>The ability to override the <code class="docutils literal notranslate"><span class="pre">popup_response.html</span></code> template was added.</p>
</div>
<p>For those templates that cannot be overridden in this way, you may still
override them for your entire project. Just place the new version in your
<code class="docutils literal notranslate"><span class="pre">templates/admin</span></code> directory. This is particularly useful to create custom 404
and 500 pages.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Some of the admin templates, such as <code class="docutils literal notranslate"><span class="pre">change_list_results.html</span></code> are used
to render custom inclusion tags. These may be overridden, but in such cases
you are probably better off creating your own version of the tag in
question and giving it a different name. That way you can use it
selectively.</p>
</div>
</div>
<div class="section" id="s-root-and-login-templates">
<span id="root-and-login-templates"></span><h3>Root and login templates<a class="headerlink" href="#root-and-login-templates" title="Permalink to this headline">¶</a></h3>
<p>If you wish to change the index, login or logout templates, you are better off
creating your own <code class="docutils literal notranslate"><span class="pre">AdminSite</span></code> instance (see below), and changing the
<a class="reference internal" href="#django.contrib.admin.AdminSite.index_template" title="django.contrib.admin.AdminSite.index_template"><code class="xref py py-attr docutils literal notranslate"><span class="pre">AdminSite.index_template</span></code></a> , <a class="reference internal" href="#django.contrib.admin.AdminSite.login_template" title="django.contrib.admin.AdminSite.login_template"><code class="xref py py-attr docutils literal notranslate"><span class="pre">AdminSite.login_template</span></code></a> or
<a class="reference internal" href="#django.contrib.admin.AdminSite.logout_template" title="django.contrib.admin.AdminSite.logout_template"><code class="xref py py-attr docutils literal notranslate"><span class="pre">AdminSite.logout_template</span></code></a> properties.</p>
</div>
</div>
<div class="section" id="s-adminsite-objects">
<span id="adminsite-objects"></span><h2><code class="docutils literal notranslate"><span class="pre">AdminSite</span></code> objects<a class="headerlink" href="#adminsite-objects" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="django.contrib.admin.AdminSite">
<em class="property">class </em><code class="descname">AdminSite</code>(<em>name='admin'</em>)<a class="reference internal" href="../../../_modules/django/contrib/admin/sites.html#AdminSite"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.contrib.admin.AdminSite" title="Permalink to this definition">¶</a></dt>
<dd><p>A Django administrative site is represented by an instance of
<code class="docutils literal notranslate"><span class="pre">django.contrib.admin.sites.AdminSite</span></code>; by default, an instance of
this class is created as <code class="docutils literal notranslate"><span class="pre">django.contrib.admin.site</span></code> and you can
register your models and <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> instances with it.</p>
<p>When constructing an instance of an <code class="docutils literal notranslate"><span class="pre">AdminSite</span></code>, you can provide
a unique instance name using the <code class="docutils literal notranslate"><span class="pre">name</span></code> argument to the constructor. This
instance name is used to identify the instance, especially when
<a class="reference internal" href="#admin-reverse-urls"><span class="std std-ref">reversing admin URLs</span></a>. If no instance name is
provided, a default instance name of <code class="docutils literal notranslate"><span class="pre">admin</span></code> will be used.
See <a class="reference internal" href="#customizing-adminsite"><span class="std std-ref">Customizing the AdminSite class</span></a> for an example of customizing the
<a class="reference internal" href="#django.contrib.admin.AdminSite" title="django.contrib.admin.AdminSite"><code class="xref py py-class docutils literal notranslate"><span class="pre">AdminSite</span></code></a> class.</p>
</dd></dl>

<div class="section" id="s-adminsite-attributes">
<span id="adminsite-attributes"></span><h3><code class="docutils literal notranslate"><span class="pre">AdminSite</span></code> attributes<a class="headerlink" href="#adminsite-attributes" title="Permalink to this headline">¶</a></h3>
<p>Templates can override or extend base admin templates as described in
<a class="reference internal" href="#admin-overriding-templates"><span class="std std-ref">Overriding admin templates</span></a>.</p>
<dl class="attribute">
<dt id="django.contrib.admin.AdminSite.site_header">
<code class="descclassname">AdminSite.</code><code class="descname">site_header</code><a class="headerlink" href="#django.contrib.admin.AdminSite.site_header" title="Permalink to this definition">¶</a></dt>
<dd><p>The text to put at the top of each admin page, as an <code class="docutils literal notranslate"><span class="pre">&lt;h1&gt;</span></code> (a string).
By default, this is “Django administration”.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.AdminSite.site_title">
<code class="descclassname">AdminSite.</code><code class="descname">site_title</code><a class="headerlink" href="#django.contrib.admin.AdminSite.site_title" title="Permalink to this definition">¶</a></dt>
<dd><p>The text to put at the end of each admin page’s <code class="docutils literal notranslate"><span class="pre">&lt;title&gt;</span></code> (a string). By
default, this is “Django site admin”.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.AdminSite.site_url">
<code class="descclassname">AdminSite.</code><code class="descname">site_url</code><a class="headerlink" href="#django.contrib.admin.AdminSite.site_url" title="Permalink to this definition">¶</a></dt>
<dd><p>The URL for the “View site” link at the top of each admin page. By default,
<code class="docutils literal notranslate"><span class="pre">site_url</span></code> is <code class="docutils literal notranslate"><span class="pre">/</span></code>. Set it to <code class="docutils literal notranslate"><span class="pre">None</span></code> to remove the link.</p>
<p>For sites running on a subpath, the <a class="reference internal" href="#django.contrib.admin.AdminSite.each_context" title="django.contrib.admin.AdminSite.each_context"><code class="xref py py-meth docutils literal notranslate"><span class="pre">each_context()</span></code></a> method checks if
the current request has <code class="docutils literal notranslate"><span class="pre">request.META['SCRIPT_NAME']</span></code> set and uses that
value if <code class="docutils literal notranslate"><span class="pre">site_url</span></code> isn’t set to something other than <code class="docutils literal notranslate"><span class="pre">/</span></code>.</p>
<div class="versionchanged">
<span class="title">Changed in Django 1.10:</span> <p>The <code class="docutils literal notranslate"><span class="pre">SCRIPT_NAME</span></code> support described in the previous paragraph was
added.</p>
</div>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.AdminSite.index_title">
<code class="descclassname">AdminSite.</code><code class="descname">index_title</code><a class="headerlink" href="#django.contrib.admin.AdminSite.index_title" title="Permalink to this definition">¶</a></dt>
<dd><p>The text to put at the top of the admin index page (a string). By default,
this is “Site administration”.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.AdminSite.index_template">
<code class="descclassname">AdminSite.</code><code class="descname">index_template</code><a class="headerlink" href="#django.contrib.admin.AdminSite.index_template" title="Permalink to this definition">¶</a></dt>
<dd><p>Path to a custom template that will be used by the admin site main index
view.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.AdminSite.app_index_template">
<code class="descclassname">AdminSite.</code><code class="descname">app_index_template</code><a class="headerlink" href="#django.contrib.admin.AdminSite.app_index_template" title="Permalink to this definition">¶</a></dt>
<dd><p>Path to a custom template that will be used by the admin site app index view.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.AdminSite.empty_value_display">
<code class="descclassname">AdminSite.</code><code class="descname">empty_value_display</code><a class="headerlink" href="#django.contrib.admin.AdminSite.empty_value_display" title="Permalink to this definition">¶</a></dt>
<dd><p>The string to use for displaying empty values in the admin site’s change
list. Defaults to a dash. The value can also be overridden on a per
<code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> basis and on a custom field within a <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> by
setting an <code class="docutils literal notranslate"><span class="pre">empty_value_display</span></code> attribute on the field. See
<a class="reference internal" href="#django.contrib.admin.ModelAdmin.empty_value_display" title="django.contrib.admin.ModelAdmin.empty_value_display"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ModelAdmin.empty_value_display</span></code></a> for examples.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.AdminSite.login_template">
<code class="descclassname">AdminSite.</code><code class="descname">login_template</code><a class="headerlink" href="#django.contrib.admin.AdminSite.login_template" title="Permalink to this definition">¶</a></dt>
<dd><p>Path to a custom template that will be used by the admin site login view.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.AdminSite.login_form">
<code class="descclassname">AdminSite.</code><code class="descname">login_form</code><a class="headerlink" href="#django.contrib.admin.AdminSite.login_form" title="Permalink to this definition">¶</a></dt>
<dd><p>Subclass of <a class="reference internal" href="../../../topics/auth/default.html#django.contrib.auth.forms.AuthenticationForm" title="django.contrib.auth.forms.AuthenticationForm"><code class="xref py py-class docutils literal notranslate"><span class="pre">AuthenticationForm</span></code></a> that
will be used by the admin site login view.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.AdminSite.logout_template">
<code class="descclassname">AdminSite.</code><code class="descname">logout_template</code><a class="headerlink" href="#django.contrib.admin.AdminSite.logout_template" title="Permalink to this definition">¶</a></dt>
<dd><p>Path to a custom template that will be used by the admin site logout view.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.AdminSite.password_change_template">
<code class="descclassname">AdminSite.</code><code class="descname">password_change_template</code><a class="headerlink" href="#django.contrib.admin.AdminSite.password_change_template" title="Permalink to this definition">¶</a></dt>
<dd><p>Path to a custom template that will be used by the admin site password
change view.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.AdminSite.password_change_done_template">
<code class="descclassname">AdminSite.</code><code class="descname">password_change_done_template</code><a class="headerlink" href="#django.contrib.admin.AdminSite.password_change_done_template" title="Permalink to this definition">¶</a></dt>
<dd><p>Path to a custom template that will be used by the admin site password
change done view.</p>
</dd></dl>

</div>
<div class="section" id="s-adminsite-methods">
<span id="adminsite-methods"></span><h3><code class="docutils literal notranslate"><span class="pre">AdminSite</span></code> methods<a class="headerlink" href="#adminsite-methods" title="Permalink to this headline">¶</a></h3>
<dl class="method">
<dt id="django.contrib.admin.AdminSite.each_context">
<code class="descclassname">AdminSite.</code><code class="descname">each_context</code>(<em>request</em>)<a class="reference internal" href="../../../_modules/django/contrib/admin/sites.html#AdminSite.each_context"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.contrib.admin.AdminSite.each_context" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a dictionary of variables to put in the template context for
every page in the admin site.</p>
<p>Includes the following variables and values by default:</p>
<ul>
<li><p class="first"><code class="docutils literal notranslate"><span class="pre">site_header</span></code>: <a class="reference internal" href="#django.contrib.admin.AdminSite.site_header" title="django.contrib.admin.AdminSite.site_header"><code class="xref py py-attr docutils literal notranslate"><span class="pre">AdminSite.site_header</span></code></a></p>
</li>
<li><p class="first"><code class="docutils literal notranslate"><span class="pre">site_title</span></code>: <a class="reference internal" href="#django.contrib.admin.AdminSite.site_title" title="django.contrib.admin.AdminSite.site_title"><code class="xref py py-attr docutils literal notranslate"><span class="pre">AdminSite.site_title</span></code></a></p>
</li>
<li><p class="first"><code class="docutils literal notranslate"><span class="pre">site_url</span></code>: <a class="reference internal" href="#django.contrib.admin.AdminSite.site_url" title="django.contrib.admin.AdminSite.site_url"><code class="xref py py-attr docutils literal notranslate"><span class="pre">AdminSite.site_url</span></code></a></p>
</li>
<li><p class="first"><code class="docutils literal notranslate"><span class="pre">has_permission</span></code>: <a class="reference internal" href="#django.contrib.admin.AdminSite.has_permission" title="django.contrib.admin.AdminSite.has_permission"><code class="xref py py-meth docutils literal notranslate"><span class="pre">AdminSite.has_permission()</span></code></a></p>
</li>
<li><p class="first"><code class="docutils literal notranslate"><span class="pre">available_apps</span></code>: a list of applications from the <a class="reference internal" href="../../applications.html"><span class="doc">application registry</span></a> available for the current user. Each entry in the
list is a dict representing an application with the following keys:</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">app_label</span></code>: the application label</li>
<li><code class="docutils literal notranslate"><span class="pre">app_url</span></code>: the URL of the application index in the admin</li>
<li><code class="docutils literal notranslate"><span class="pre">has_module_perms</span></code>: a boolean indicating if displaying and accessing of
the module’s index page is permitted for the current user</li>
<li><code class="docutils literal notranslate"><span class="pre">models</span></code>: a list of the models available in the application</li>
</ul>
<p>Each model is a dict with the following keys:</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">object_name</span></code>: class name of the model</li>
<li><code class="docutils literal notranslate"><span class="pre">name</span></code>: plural name of the model</li>
<li><code class="docutils literal notranslate"><span class="pre">perms</span></code>: a <code class="docutils literal notranslate"><span class="pre">dict</span></code> tracking <code class="docutils literal notranslate"><span class="pre">add</span></code>, <code class="docutils literal notranslate"><span class="pre">change</span></code>, and <code class="docutils literal notranslate"><span class="pre">delete</span></code> permissions</li>
<li><code class="docutils literal notranslate"><span class="pre">admin_url</span></code>: admin changelist URL for the model</li>
<li><code class="docutils literal notranslate"><span class="pre">add_url</span></code>: admin URL to add a new model instance</li>
</ul>
</li>
</ul>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.AdminSite.has_permission">
<code class="descclassname">AdminSite.</code><code class="descname">has_permission</code>(<em>request</em>)<a class="reference internal" href="../../../_modules/django/contrib/admin/sites.html#AdminSite.has_permission"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.contrib.admin.AdminSite.has_permission" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns <code class="docutils literal notranslate"><span class="pre">True</span></code> if the user for the given <code class="docutils literal notranslate"><span class="pre">HttpRequest</span></code> has permission
to view at least one page in the admin site. Defaults to requiring both
<a class="reference internal" href="../auth.html#django.contrib.auth.models.User.is_active" title="django.contrib.auth.models.User.is_active"><code class="xref py py-attr docutils literal notranslate"><span class="pre">User.is_active</span></code></a> and
<a class="reference internal" href="../auth.html#django.contrib.auth.models.User.is_staff" title="django.contrib.auth.models.User.is_staff"><code class="xref py py-attr docutils literal notranslate"><span class="pre">User.is_staff</span></code></a> to be
<code class="docutils literal notranslate"><span class="pre">True</span></code>.</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.AdminSite.register">
<code class="descclassname">AdminSite.</code><code class="descname">register</code>(<em>model_or_iterable</em>, <em>admin_class=None</em>, <em>**options</em>)<a class="reference internal" href="../../../_modules/django/contrib/admin/sites.html#AdminSite.register"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.contrib.admin.AdminSite.register" title="Permalink to this definition">¶</a></dt>
<dd><p>Registers the given model class (or iterable of classes) with the given
<code class="docutils literal notranslate"><span class="pre">admin_class</span></code>. <code class="docutils literal notranslate"><span class="pre">admin_class</span></code> defaults to
<a class="reference internal" href="#django.contrib.admin.ModelAdmin" title="django.contrib.admin.ModelAdmin"><code class="xref py py-class docutils literal notranslate"><span class="pre">ModelAdmin</span></code></a> (the default admin options). If
keyword arguments are given – e.g. <code class="docutils literal notranslate"><span class="pre">list_display</span></code> – they’ll be applied
as options to the admin class.</p>
<p>Raises <a class="reference internal" href="../../exceptions.html#django.core.exceptions.ImproperlyConfigured" title="django.core.exceptions.ImproperlyConfigured"><code class="xref py py-class docutils literal notranslate"><span class="pre">ImproperlyConfigured</span></code></a> if a model is
abstract. and <code class="docutils literal notranslate"><span class="pre">django.contrib.admin.sites.AlreadyRegistered</span></code> if a model
is already registered.</p>
</dd></dl>

</div>
<div class="section" id="s-hooking-adminsite-instances-into-your-urlconf">
<span id="hooking-adminsite-instances-into-your-urlconf"></span><h3>Hooking <code class="docutils literal notranslate"><span class="pre">AdminSite</span></code> instances into your URLconf<a class="headerlink" href="#hooking-adminsite-instances-into-your-urlconf" title="Permalink to this headline">¶</a></h3>
<p>The last step in setting up the Django admin is to hook your <code class="docutils literal notranslate"><span class="pre">AdminSite</span></code>
instance into your URLconf. Do this by pointing a given URL at the
<code class="docutils literal notranslate"><span class="pre">AdminSite.urls</span></code> method. It is not necessary to use
<a class="reference internal" href="../../urls.html#django.conf.urls.include" title="django.conf.urls.include"><code class="xref py py-func docutils literal notranslate"><span class="pre">include()</span></code></a>.</p>
<p>In this example, we register the default <code class="docutils literal notranslate"><span class="pre">AdminSite</span></code> instance
<code class="docutils literal notranslate"><span class="pre">django.contrib.admin.site</span></code> at the URL <code class="docutils literal notranslate"><span class="pre">/admin/</span></code></p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># urls.py</span>
<span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="k">import</span> <span class="n">url</span>
<span class="kn">from</span> <span class="nn">django.contrib</span> <span class="k">import</span> <span class="n">admin</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^admin/&#39;</span><span class="p">,</span> <span class="n">admin</span><span class="o">.</span><span class="n">site</span><span class="o">.</span><span class="n">urls</span><span class="p">),</span>
<span class="p">]</span>
</pre></div>
</div>
</div>
<div class="section" id="s-customizing-the-adminsite-class">
<span id="s-customizing-adminsite"></span><span id="customizing-the-adminsite-class"></span><span id="customizing-adminsite"></span><h3>Customizing the <a class="reference internal" href="#django.contrib.admin.AdminSite" title="django.contrib.admin.AdminSite"><code class="xref py py-class docutils literal notranslate"><span class="pre">AdminSite</span></code></a> class<a class="headerlink" href="#customizing-the-adminsite-class" title="Permalink to this headline">¶</a></h3>
<p>If you’d like to set up your own admin site with custom behavior, you’re free
to subclass <code class="docutils literal notranslate"><span class="pre">AdminSite</span></code> and override or add anything you like. Then, simply
create an instance of your <code class="docutils literal notranslate"><span class="pre">AdminSite</span></code> subclass (the same way you’d
instantiate any other Python class) and register your models and
<code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> subclasses with it instead of with the default site. Finally,
update <code class="file docutils literal notranslate"><span class="pre">myproject/urls.py</span></code> to reference your <a class="reference internal" href="#django.contrib.admin.AdminSite" title="django.contrib.admin.AdminSite"><code class="xref py py-class docutils literal notranslate"><span class="pre">AdminSite</span></code></a> subclass.</p>
<div class="highlight-default snippet"><div class="snippet-filename">myapp/admin.py</div>
<div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib.admin</span> <span class="k">import</span> <span class="n">AdminSite</span>

<span class="kn">from</span> <span class="nn">.models</span> <span class="k">import</span> <span class="n">MyModel</span>

<span class="k">class</span> <span class="nc">MyAdminSite</span><span class="p">(</span><span class="n">AdminSite</span><span class="p">):</span>
    <span class="n">site_header</span> <span class="o">=</span> <span class="s1">&#39;Monty Python administration&#39;</span>

<span class="n">admin_site</span> <span class="o">=</span> <span class="n">MyAdminSite</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">&#39;myadmin&#39;</span><span class="p">)</span>
<span class="n">admin_site</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">MyModel</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-default snippet"><div class="snippet-filename">myproject/urls.py</div>
<div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="k">import</span> <span class="n">url</span>

<span class="kn">from</span> <span class="nn">myapp.admin</span> <span class="k">import</span> <span class="n">admin_site</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^myadmin/&#39;</span><span class="p">,</span> <span class="n">admin_site</span><span class="o">.</span><span class="n">urls</span><span class="p">),</span>
<span class="p">]</span>
</pre></div>
</div>
<p>Note that you may not want autodiscovery of <code class="docutils literal notranslate"><span class="pre">admin</span></code> modules when using your
own <code class="docutils literal notranslate"><span class="pre">AdminSite</span></code> instance since you will likely be importing all the per-app
<code class="docutils literal notranslate"><span class="pre">admin</span></code> modules in your <code class="docutils literal notranslate"><span class="pre">myproject.admin</span></code> module. This means you need to
put <code class="docutils literal notranslate"><span class="pre">'django.contrib.admin.apps.SimpleAdminConfig'</span></code> instead of
<code class="docutils literal notranslate"><span class="pre">'django.contrib.admin'</span></code> in your <a class="reference internal" href="../../settings.html#std:setting-INSTALLED_APPS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">INSTALLED_APPS</span></code></a> setting.</p>
</div>
<div class="section" id="s-multiple-admin-sites-in-the-same-urlconf">
<span id="s-multiple-admin-sites"></span><span id="multiple-admin-sites-in-the-same-urlconf"></span><span id="multiple-admin-sites"></span><h3>Multiple admin sites in the same URLconf<a class="headerlink" href="#multiple-admin-sites-in-the-same-urlconf" title="Permalink to this headline">¶</a></h3>
<p>It’s easy to create multiple instances of the admin site on the same
Django-powered website. Just create multiple instances of <code class="docutils literal notranslate"><span class="pre">AdminSite</span></code> and
root each one at a different URL.</p>
<p>In this example, the URLs <code class="docutils literal notranslate"><span class="pre">/basic-admin/</span></code> and <code class="docutils literal notranslate"><span class="pre">/advanced-admin/</span></code> feature
separate versions of the admin site – using the <code class="docutils literal notranslate"><span class="pre">AdminSite</span></code> instances
<code class="docutils literal notranslate"><span class="pre">myproject.admin.basic_site</span></code> and <code class="docutils literal notranslate"><span class="pre">myproject.admin.advanced_site</span></code>,
respectively:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># urls.py</span>
<span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="k">import</span> <span class="n">url</span>
<span class="kn">from</span> <span class="nn">myproject.admin</span> <span class="k">import</span> <span class="n">basic_site</span><span class="p">,</span> <span class="n">advanced_site</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^basic-admin/&#39;</span><span class="p">,</span> <span class="n">basic_site</span><span class="o">.</span><span class="n">urls</span><span class="p">),</span>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^advanced-admin/&#39;</span><span class="p">,</span> <span class="n">advanced_site</span><span class="o">.</span><span class="n">urls</span><span class="p">),</span>
<span class="p">]</span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">AdminSite</span></code> instances take a single argument to their constructor, their
name, which can be anything you like. This argument becomes the prefix to the
URL names for the purposes of <a class="reference internal" href="#admin-reverse-urls"><span class="std std-ref">reversing them</span></a>. This
is only necessary if you are using more than one <code class="docutils literal notranslate"><span class="pre">AdminSite</span></code>.</p>
</div>
<div class="section" id="s-adding-views-to-admin-sites">
<span id="adding-views-to-admin-sites"></span><h3>Adding views to admin sites<a class="headerlink" href="#adding-views-to-admin-sites" title="Permalink to this headline">¶</a></h3>
<p>Just like <a class="reference internal" href="#django.contrib.admin.ModelAdmin" title="django.contrib.admin.ModelAdmin"><code class="xref py py-class docutils literal notranslate"><span class="pre">ModelAdmin</span></code></a>, <a class="reference internal" href="#django.contrib.admin.AdminSite" title="django.contrib.admin.AdminSite"><code class="xref py py-class docutils literal notranslate"><span class="pre">AdminSite</span></code></a> provides a
<a class="reference internal" href="#django.contrib.admin.ModelAdmin.get_urls" title="django.contrib.admin.ModelAdmin.get_urls"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_urls()</span></code></a> method
that can be overridden to define additional views for the site. To add
a new view to your admin site, extend the base
<a class="reference internal" href="#django.contrib.admin.ModelAdmin.get_urls" title="django.contrib.admin.ModelAdmin.get_urls"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_urls()</span></code></a> method to include
a pattern for your new view.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Any view you render that uses the admin templates, or extends the base
admin template, should set <code class="docutils literal notranslate"><span class="pre">request.current_app</span></code> before rendering the
template. It should be set to either <code class="docutils literal notranslate"><span class="pre">self.name</span></code> if your view is on an
<code class="docutils literal notranslate"><span class="pre">AdminSite</span></code> or <code class="docutils literal notranslate"><span class="pre">self.admin_site.name</span></code> if your view is on a
<code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code>.</p>
</div>
</div>
<div class="section" id="s-adding-a-password-reset-feature">
<span id="s-auth-password-reset"></span><span id="adding-a-password-reset-feature"></span><span id="auth-password-reset"></span><h3>Adding a password reset feature<a class="headerlink" href="#adding-a-password-reset-feature" title="Permalink to this headline">¶</a></h3>
<p>You can add a password reset feature to the admin site by adding a few lines to
your URLconf. Specifically, add these four patterns:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib.auth</span> <span class="k">import</span> <span class="n">views</span> <span class="k">as</span> <span class="n">auth_views</span>

<span class="n">url</span><span class="p">(</span>
    <span class="sa">r</span><span class="s1">&#39;^admin/password_reset/$&#39;</span><span class="p">,</span>
    <span class="n">auth_views</span><span class="o">.</span><span class="n">PasswordResetView</span><span class="o">.</span><span class="n">as_view</span><span class="p">(),</span>
    <span class="n">name</span><span class="o">=</span><span class="s1">&#39;admin_password_reset&#39;</span><span class="p">,</span>
<span class="p">),</span>
<span class="n">url</span><span class="p">(</span>
    <span class="sa">r</span><span class="s1">&#39;^admin/password_reset/done/$&#39;</span><span class="p">,</span>
    <span class="n">auth_views</span><span class="o">.</span><span class="n">PasswordResetDoneView</span><span class="o">.</span><span class="n">as_view</span><span class="p">(),</span>
    <span class="n">name</span><span class="o">=</span><span class="s1">&#39;password_reset_done&#39;</span><span class="p">,</span>
<span class="p">),</span>
<span class="n">url</span><span class="p">(</span>
    <span class="sa">r</span><span class="s1">&#39;^reset/(?P&lt;uidb64&gt;[0-9A-Za-z_\-]+)/(?P&lt;token&gt;.+)/$&#39;</span><span class="p">,</span>
    <span class="n">auth_views</span><span class="o">.</span><span class="n">PasswordResetConfirmView</span><span class="o">.</span><span class="n">as_view</span><span class="p">(),</span>
    <span class="n">name</span><span class="o">=</span><span class="s1">&#39;password_reset_confirm&#39;</span><span class="p">,</span>
<span class="p">),</span>
<span class="n">url</span><span class="p">(</span>
    <span class="sa">r</span><span class="s1">&#39;^reset/done/$&#39;</span><span class="p">,</span>
    <span class="n">auth_views</span><span class="o">.</span><span class="n">PasswordResetCompleteView</span><span class="o">.</span><span class="n">as_view</span><span class="p">(),</span>
    <span class="n">name</span><span class="o">=</span><span class="s1">&#39;password_reset_complete&#39;</span><span class="p">,</span>
<span class="p">),</span>
</pre></div>
</div>
<p>(This assumes you’ve added the admin at <code class="docutils literal notranslate"><span class="pre">admin/</span></code> and requires that you put
the URLs starting with <code class="docutils literal notranslate"><span class="pre">^admin/</span></code> before the line that includes the admin app
itself).</p>
<p>The presence of the <code class="docutils literal notranslate"><span class="pre">admin_password_reset</span></code> named URL will cause a “forgotten
your password?” link to appear on the default admin log-in page under the
password box.</p>
</div>
</div>
<div class="section" id="s-logentry-objects">
<span id="logentry-objects"></span><h2><code class="docutils literal notranslate"><span class="pre">LogEntry</span></code> objects<a class="headerlink" href="#logentry-objects" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="django.contrib.admin.models.LogEntry">
<em class="property">class </em><code class="descclassname">models.</code><code class="descname">LogEntry</code><a class="headerlink" href="#django.contrib.admin.models.LogEntry" title="Permalink to this definition">¶</a></dt>
<dd><p>The <code class="docutils literal notranslate"><span class="pre">LogEntry</span></code> class tracks additions, changes, and deletions of objects
done through the admin interface.</p>
</dd></dl>

<div class="section" id="s-logentry-attributes">
<span id="logentry-attributes"></span><h3><code class="docutils literal notranslate"><span class="pre">LogEntry</span></code> attributes<a class="headerlink" href="#logentry-attributes" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.contrib.admin.models.LogEntry.action_time">
<code class="descclassname">LogEntry.</code><code class="descname">action_time</code><a class="headerlink" href="#django.contrib.admin.models.LogEntry.action_time" title="Permalink to this definition">¶</a></dt>
<dd><p>The date and time of the action.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.models.LogEntry.user">
<code class="descclassname">LogEntry.</code><code class="descname">user</code><a class="headerlink" href="#django.contrib.admin.models.LogEntry.user" title="Permalink to this definition">¶</a></dt>
<dd><p>The user (an <a class="reference internal" href="../../settings.html#std:setting-AUTH_USER_MODEL"><code class="xref std std-setting docutils literal notranslate"><span class="pre">AUTH_USER_MODEL</span></code></a> instance) who performed the
action.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.models.LogEntry.content_type">
<code class="descclassname">LogEntry.</code><code class="descname">content_type</code><a class="headerlink" href="#django.contrib.admin.models.LogEntry.content_type" title="Permalink to this definition">¶</a></dt>
<dd><p>The <a class="reference internal" href="../contenttypes.html#django.contrib.contenttypes.models.ContentType" title="django.contrib.contenttypes.models.ContentType"><code class="xref py py-class docutils literal notranslate"><span class="pre">ContentType</span></code></a> of the
modified object.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.models.LogEntry.object_id">
<code class="descclassname">LogEntry.</code><code class="descname">object_id</code><a class="headerlink" href="#django.contrib.admin.models.LogEntry.object_id" title="Permalink to this definition">¶</a></dt>
<dd><p>The textual representation of the modified object’s primary key.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.models.LogEntry.object_repr">
<code class="descclassname">LogEntry.</code><code class="descname">object_repr</code><a class="headerlink" href="#django.contrib.admin.models.LogEntry.object_repr" title="Permalink to this definition">¶</a></dt>
<dd><p>The object`s <code class="docutils literal notranslate"><span class="pre">repr()</span></code> after the modification.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.models.LogEntry.action_flag">
<code class="descclassname">LogEntry.</code><code class="descname">action_flag</code><a class="headerlink" href="#django.contrib.admin.models.LogEntry.action_flag" title="Permalink to this definition">¶</a></dt>
<dd><p>The type of action logged: <code class="docutils literal notranslate"><span class="pre">ADDITION</span></code>, <code class="docutils literal notranslate"><span class="pre">CHANGE</span></code>, <code class="docutils literal notranslate"><span class="pre">DELETION</span></code>.</p>
<p>For example, to get a list of all additions done through the admin:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib.admin.models</span> <span class="k">import</span> <span class="n">LogEntry</span><span class="p">,</span> <span class="n">ADDITION</span>

<span class="n">LogEntry</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">action_flag</span><span class="o">=</span><span class="n">ADDITION</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.models.LogEntry.change_message">
<code class="descclassname">LogEntry.</code><code class="descname">change_message</code><a class="headerlink" href="#django.contrib.admin.models.LogEntry.change_message" title="Permalink to this definition">¶</a></dt>
<dd><p>The detailed description of the modification. In the case of an edit, for
example, the message contains a list of the edited fields. The Django admin
site formats this content as a JSON structure, so that
<a class="reference internal" href="#django.contrib.admin.models.LogEntry.get_change_message" title="django.contrib.admin.models.LogEntry.get_change_message"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_change_message()</span></code></a> can recompose a message translated in the current
user language. Custom code might set this as a plain string though. You are
advised to use the <a class="reference internal" href="#django.contrib.admin.models.LogEntry.get_change_message" title="django.contrib.admin.models.LogEntry.get_change_message"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_change_message()</span></code></a> method to retrieve this value
instead of accessing it directly.</p>
<div class="versionchanged">
<span class="title">Changed in Django 1.10:</span> <p>Previously, this attribute was always a plain string. It is
now JSON-structured so that the message can be translated in the current
user language. Old messages are untouched.</p>
</div>
</dd></dl>

</div>
<div class="section" id="s-logentry-methods">
<span id="logentry-methods"></span><h3><code class="docutils literal notranslate"><span class="pre">LogEntry</span></code> methods<a class="headerlink" href="#logentry-methods" title="Permalink to this headline">¶</a></h3>
<dl class="method">
<dt id="django.contrib.admin.models.LogEntry.get_edited_object">
<code class="descclassname">LogEntry.</code><code class="descname">get_edited_object</code>()<a class="headerlink" href="#django.contrib.admin.models.LogEntry.get_edited_object" title="Permalink to this definition">¶</a></dt>
<dd><p>A shortcut that returns the referenced object.</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.models.LogEntry.get_change_message">
<code class="descclassname">LogEntry.</code><code class="descname">get_change_message</code>()<a class="headerlink" href="#django.contrib.admin.models.LogEntry.get_change_message" title="Permalink to this definition">¶</a></dt>
<dd><div class="versionadded">
<span class="title">New in Django 1.10.</span> </div>
<p>Formats and translates <a class="reference internal" href="#django.contrib.admin.models.LogEntry.change_message" title="django.contrib.admin.models.LogEntry.change_message"><code class="xref py py-attr docutils literal notranslate"><span class="pre">change_message</span></code></a> into the current user
language. Messages created before Django 1.10 will always be displayed in
the language in which they were logged.</p>
</dd></dl>

</div>
</div>
<div class="section" id="s-reversing-admin-urls">
<span id="s-admin-reverse-urls"></span><span id="reversing-admin-urls"></span><span id="admin-reverse-urls"></span><h2>Reversing admin URLs<a class="headerlink" href="#reversing-admin-urls" title="Permalink to this headline">¶</a></h2>
<p>When an <a class="reference internal" href="#django.contrib.admin.AdminSite" title="django.contrib.admin.AdminSite"><code class="xref py py-class docutils literal notranslate"><span class="pre">AdminSite</span></code></a> is deployed, the views provided by that site are
accessible using Django’s <a class="reference internal" href="../../../topics/http/urls.html#naming-url-patterns"><span class="std std-ref">URL reversing system</span></a>.</p>
<p>The <a class="reference internal" href="#django.contrib.admin.AdminSite" title="django.contrib.admin.AdminSite"><code class="xref py py-class docutils literal notranslate"><span class="pre">AdminSite</span></code></a> provides the following named URL patterns:</p>
<table class="docutils">
<colgroup>
<col width="30%" />
<col width="29%" />
<col width="41%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Page</th>
<th class="head">URL name</th>
<th class="head">Parameters</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>Index</td>
<td><code class="docutils literal notranslate"><span class="pre">index</span></code></td>
<td>&#160;</td>
</tr>
<tr class="row-odd"><td>Login</td>
<td><code class="docutils literal notranslate"><span class="pre">login</span></code></td>
<td>&#160;</td>
</tr>
<tr class="row-even"><td>Logout</td>
<td><code class="docutils literal notranslate"><span class="pre">logout</span></code></td>
<td>&#160;</td>
</tr>
<tr class="row-odd"><td>Password change</td>
<td><code class="docutils literal notranslate"><span class="pre">password_change</span></code></td>
<td>&#160;</td>
</tr>
<tr class="row-even"><td>Password change done</td>
<td><code class="docutils literal notranslate"><span class="pre">password_change_done</span></code></td>
<td>&#160;</td>
</tr>
<tr class="row-odd"><td>i18n JavaScript</td>
<td><code class="docutils literal notranslate"><span class="pre">jsi18n</span></code></td>
<td>&#160;</td>
</tr>
<tr class="row-even"><td>Application index page</td>
<td><code class="docutils literal notranslate"><span class="pre">app_list</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">app_label</span></code></td>
</tr>
<tr class="row-odd"><td>Redirect to object’s page</td>
<td><code class="docutils literal notranslate"><span class="pre">view_on_site</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">content_type_id</span></code>, <code class="docutils literal notranslate"><span class="pre">object_id</span></code></td>
</tr>
</tbody>
</table>
<p>Each <a class="reference internal" href="#django.contrib.admin.ModelAdmin" title="django.contrib.admin.ModelAdmin"><code class="xref py py-class docutils literal notranslate"><span class="pre">ModelAdmin</span></code></a> instance provides an additional set of named URLs:</p>
<table class="docutils">
<colgroup>
<col width="27%" />
<col width="57%" />
<col width="16%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Page</th>
<th class="head">URL name</th>
<th class="head">Parameters</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>Changelist</td>
<td><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">app_label</span> <span class="pre">}}_{{</span> <span class="pre">model_name</span> <span class="pre">}}_changelist</span></code></td>
<td>&#160;</td>
</tr>
<tr class="row-odd"><td>Add</td>
<td><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">app_label</span> <span class="pre">}}_{{</span> <span class="pre">model_name</span> <span class="pre">}}_add</span></code></td>
<td>&#160;</td>
</tr>
<tr class="row-even"><td>History</td>
<td><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">app_label</span> <span class="pre">}}_{{</span> <span class="pre">model_name</span> <span class="pre">}}_history</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">object_id</span></code></td>
</tr>
<tr class="row-odd"><td>Delete</td>
<td><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">app_label</span> <span class="pre">}}_{{</span> <span class="pre">model_name</span> <span class="pre">}}_delete</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">object_id</span></code></td>
</tr>
<tr class="row-even"><td>Change</td>
<td><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">app_label</span> <span class="pre">}}_{{</span> <span class="pre">model_name</span> <span class="pre">}}_change</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">object_id</span></code></td>
</tr>
</tbody>
</table>
<p>The <code class="docutils literal notranslate"><span class="pre">UserAdmin</span></code> provides a named URL:</p>
<table class="docutils">
<colgroup>
<col width="27%" />
<col width="57%" />
<col width="16%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Page</th>
<th class="head">URL name</th>
<th class="head">Parameters</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>Password change</td>
<td><code class="docutils literal notranslate"><span class="pre">auth_user_password_change</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">user_id</span></code></td>
</tr>
</tbody>
</table>
<p>These named URLs are registered with the application namespace <code class="docutils literal notranslate"><span class="pre">admin</span></code>, and
with an instance namespace corresponding to the name of the Site instance.</p>
<p>So - if you wanted to get a reference to the Change view for a particular
<code class="docutils literal notranslate"><span class="pre">Choice</span></code> object (from the polls application) in the default admin, you would
call:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.urls</span> <span class="k">import</span> <span class="n">reverse</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">=</span> <span class="n">Choice</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="o">...</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">change_url</span> <span class="o">=</span> <span class="n">reverse</span><span class="p">(</span><span class="s1">&#39;admin:polls_choice_change&#39;</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">id</span><span class="p">,))</span>
</pre></div>
</div>
<p>This will find the first registered instance of the admin application
(whatever the instance name), and resolve to the view for changing
<code class="docutils literal notranslate"><span class="pre">poll.Choice</span></code> instances in that instance.</p>
<p>If you want to find a URL in a specific admin instance, provide the name of
that instance as a <code class="docutils literal notranslate"><span class="pre">current_app</span></code> hint to the reverse call. For example,
if you specifically wanted the admin view from the admin instance named
<code class="docutils literal notranslate"><span class="pre">custom</span></code>, you would need to call:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">change_url</span> <span class="o">=</span> <span class="n">reverse</span><span class="p">(</span><span class="s1">&#39;admin:polls_choice_change&#39;</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">id</span><span class="p">,),</span> <span class="n">current_app</span><span class="o">=</span><span class="s1">&#39;custom&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>For more details, see the documentation on <a class="reference internal" href="../../../topics/http/urls.html#topics-http-reversing-url-namespaces"><span class="std std-ref">reversing namespaced URLs</span></a>.</p>
<p>To allow easier reversing of the admin urls in templates, Django provides an
<code class="docutils literal notranslate"><span class="pre">admin_urlname</span></code> filter which takes an action as argument:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">load</span> <span class="nv">admin_urls</span> <span class="cp">%}</span>
<span class="p">&lt;</span><span class="nt">a</span> <span class="na">href</span><span class="o">=</span><span class="s">&quot;</span><span class="cp">{%</span> <span class="k">url</span> <span class="nv">opts</span><span class="o">|</span><span class="nf">admin_urlname</span><span class="s1">:&#39;add&#39;</span> <span class="cp">%}</span><span class="s">&quot;</span><span class="p">&gt;</span>Add user<span class="p">&lt;/</span><span class="nt">a</span><span class="p">&gt;</span>
<span class="p">&lt;</span><span class="nt">a</span> <span class="na">href</span><span class="o">=</span><span class="s">&quot;</span><span class="cp">{%</span> <span class="k">url</span> <span class="nv">opts</span><span class="o">|</span><span class="nf">admin_urlname</span><span class="s1">:&#39;delete&#39;</span> <span class="nv">user.pk</span> <span class="cp">%}</span><span class="s">&quot;</span><span class="p">&gt;</span>Delete this user<span class="p">&lt;/</span><span class="nt">a</span><span class="p">&gt;</span>
</pre></div>
</div>
<p>The action in the examples above match the last part of the URL names for
<a class="reference internal" href="#django.contrib.admin.ModelAdmin" title="django.contrib.admin.ModelAdmin"><code class="xref py py-class docutils literal notranslate"><span class="pre">ModelAdmin</span></code></a> instances described above. The <code class="docutils literal notranslate"><span class="pre">opts</span></code> variable can be any
object which has an <code class="docutils literal notranslate"><span class="pre">app_label</span></code> and <code class="docutils literal notranslate"><span class="pre">model_name</span></code> attributes and is usually
supplied by the admin views for the current model.</p>
</div>
<div class="section" id="s-the-staff-member-required-decorator">
<span id="the-staff-member-required-decorator"></span><h2>The <code class="docutils literal notranslate"><span class="pre">staff_member_required</span></code> decorator<a class="headerlink" href="#the-staff-member-required-decorator" title="Permalink to this headline">¶</a></h2>
<dl class="function">
<dt id="django.contrib.admin.views.decorators.staff_member_required">
<code class="descname">staff_member_required</code>(<em>redirect_field_name='next'</em>, <em>login_url='admin:login'</em>)<a class="reference internal" href="../../../_modules/django/contrib/admin/views/decorators.html#staff_member_required"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.contrib.admin.views.decorators.staff_member_required" title="Permalink to this definition">¶</a></dt>
<dd><p>This decorator is used on the admin views that require authorization. A
view decorated with this function will having the following behavior:</p>
<ul class="simple">
<li>If the user is logged in, is a staff member (<code class="docutils literal notranslate"><span class="pre">User.is_staff=True</span></code>), and
is active (<code class="docutils literal notranslate"><span class="pre">User.is_active=True</span></code>), execute the view normally.</li>
<li>Otherwise, the request will be redirected to the URL specified by the
<code class="docutils literal notranslate"><span class="pre">login_url</span></code> parameter, with the originally requested path in a query
string variable specified by <code class="docutils literal notranslate"><span class="pre">redirect_field_name</span></code>. For example:
<code class="docutils literal notranslate"><span class="pre">/admin/login/?next=/admin/polls/question/3/</span></code>.</li>
</ul>
<p>Example usage:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib.admin.views.decorators</span> <span class="k">import</span> <span class="n">staff_member_required</span>

<span class="nd">@staff_member_required</span>
<span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="o">...</span>
</pre></div>
</div>
</dd></dl>

</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">The Django admin site</a><ul>
<li><a class="reference internal" href="#overview">Overview</a><ul>
<li><a class="reference internal" href="#other-topics">Other topics</a></li>
</ul>
</li>
<li><a class="reference internal" href="#modeladmin-objects"><code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> objects</a><ul>
<li><a class="reference internal" href="#the-register-decorator">The <code class="docutils literal notranslate"><span class="pre">register</span></code> decorator</a></li>
<li><a class="reference internal" href="#discovery-of-admin-files">Discovery of admin files</a></li>
<li><a class="reference internal" href="#modeladmin-options"><code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> options</a><ul>
<li><a class="reference internal" href="#custom-template-options">Custom template options</a></li>
</ul>
</li>
<li><a class="reference internal" href="#modeladmin-methods"><code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> methods</a><ul>
<li><a class="reference internal" href="#other-methods">Other methods</a></li>
</ul>
</li>
<li><a class="reference internal" href="#modeladmin-asset-definitions"><code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> asset definitions</a><ul>
<li><a class="reference internal" href="#jquery">jQuery</a></li>
</ul>
</li>
<li><a class="reference internal" href="#adding-custom-validation-to-the-admin">Adding custom validation to the admin</a></li>
</ul>
</li>
<li><a class="reference internal" href="#inlinemodeladmin-objects"><code class="docutils literal notranslate"><span class="pre">InlineModelAdmin</span></code> objects</a><ul>
<li><a class="reference internal" href="#inlinemodeladmin-options"><code class="docutils literal notranslate"><span class="pre">InlineModelAdmin</span></code> options</a></li>
<li><a class="reference internal" href="#working-with-a-model-with-two-or-more-foreign-keys-to-the-same-parent-model">Working with a model with two or more foreign keys to the same parent model</a></li>
<li><a class="reference internal" href="#working-with-many-to-many-models">Working with many-to-many models</a></li>
<li><a class="reference internal" href="#working-with-many-to-many-intermediary-models">Working with many-to-many intermediary models</a></li>
<li><a class="reference internal" href="#using-generic-relations-as-an-inline">Using generic relations as an inline</a></li>
</ul>
</li>
<li><a class="reference internal" href="#overriding-admin-templates">Overriding admin templates</a><ul>
<li><a class="reference internal" href="#set-up-your-projects-admin-template-directories">Set up your projects admin template directories</a></li>
<li><a class="reference internal" href="#overriding-vs-replacing-an-admin-template">Overriding vs. replacing an admin template</a></li>
<li><a class="reference internal" href="#templates-which-may-be-overridden-per-app-or-model">Templates which may be overridden per app or model</a></li>
<li><a class="reference internal" href="#root-and-login-templates">Root and login templates</a></li>
</ul>
</li>
<li><a class="reference internal" href="#adminsite-objects"><code class="docutils literal notranslate"><span class="pre">AdminSite</span></code> objects</a><ul>
<li><a class="reference internal" href="#adminsite-attributes"><code class="docutils literal notranslate"><span class="pre">AdminSite</span></code> attributes</a></li>
<li><a class="reference internal" href="#adminsite-methods"><code class="docutils literal notranslate"><span class="pre">AdminSite</span></code> methods</a></li>
<li><a class="reference internal" href="#hooking-adminsite-instances-into-your-urlconf">Hooking <code class="docutils literal notranslate"><span class="pre">AdminSite</span></code> instances into your URLconf</a></li>
<li><a class="reference internal" href="#customizing-the-adminsite-class">Customizing the <code class="docutils literal notranslate"><span class="pre">AdminSite</span></code> class</a></li>
<li><a class="reference internal" href="#multiple-admin-sites-in-the-same-urlconf">Multiple admin sites in the same URLconf</a></li>
<li><a class="reference internal" href="#adding-views-to-admin-sites">Adding views to admin sites</a></li>
<li><a class="reference internal" href="#adding-a-password-reset-feature">Adding a password reset feature</a></li>
</ul>
</li>
<li><a class="reference internal" href="#logentry-objects"><code class="docutils literal notranslate"><span class="pre">LogEntry</span></code> objects</a><ul>
<li><a class="reference internal" href="#logentry-attributes"><code class="docutils literal notranslate"><span class="pre">LogEntry</span></code> attributes</a></li>
<li><a class="reference internal" href="#logentry-methods"><code class="docutils literal notranslate"><span class="pre">LogEntry</span></code> methods</a></li>
</ul>
</li>
<li><a class="reference internal" href="#reversing-admin-urls">Reversing admin URLs</a></li>
<li><a class="reference internal" href="#the-staff-member-required-decorator">The <code class="docutils literal notranslate"><span class="pre">staff_member_required</span></code> decorator</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="../index.html"
                        title="previous chapter"><code class="docutils literal notranslate"><span class="pre">contrib</span></code> packages</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="actions.html"
                        title="next chapter">Admin actions</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../../../_sources/ref/contrib/admin/index.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Jun 03, 2019</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="../index.html" title="&lt;code class=&#34;docutils literal notranslate&#34;&gt;&lt;span class=&#34;pre&#34;&gt;contrib&lt;/span&gt;&lt;/code&gt; packages">previous</a>
     |
    <a href="../../index.html" title="API Reference" accesskey="U">up</a>
   |
    <a href="actions.html" title="Admin actions">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>